EgorBaranovEnjoysTyping commented on code in PR #13262:
URL: https://github.com/apache/ignite/pull/13262#discussion_r3481294689


##########
modules/commons/src/main/java/org/apache/ignite/internal/util/tostring/CircularStringBuilder.java:
##########
@@ -170,13 +157,102 @@ private CircularStringBuilder appendNull() {
         return this;
     }
 
+    /**
+     * Inserts a string into the buffer at the specified logical offset.
+     * This method is optimized to minimize the number of elements moved by 
choosing
+     * to shift elements from the closest end (left or right) to the insertion 
point.
+     *
+     * @param offset      The logical position (accounting for skipped 
characters)
+     *                    at which to insert.
+     * @param valToInsert The string to be inserted.
+     * @throws StringIndexOutOfBoundsException if the offset is invalid.
+     */
+    public void insert(int offset, String valToInsert) {
+        int curLength = length();
+        int offsetInsideBuf = offset - skipped;
+        if (offset < 0 || offsetInsideBuf > curLength)
+            throw new StringIndexOutOfBoundsException("Offset " + offset + " 
out of bounds for length " + curLength);
+        if (valToInsert == null)
+            valToInsert = "null";
+        int insertLength = valToInsert.length();
+        if (insertLength == 0)
+            return;
+        if (offsetInsideBuf == curLength) {
+            append(valToInsert);
+            return;
+        }
+        int spareSpace = value.length - curLength;
+        int insertCnt = Math.min(valToInsert.length(), spareSpace + 
offsetInsideBuf);
+        if (insertCnt <= 0) {
+            skipped += valToInsert.length();
+            return;
+        }
+        int bufStartShiftedOffset = full ? (finishAt + 1) % value.length : 0;
+        int shiftedOffset = (bufStartShiftedOffset + offsetInsideBuf) % 
value.length;
+        int moveRightCnt = ((shiftedOffset <= finishAt ? 0 : curLength) + 
finishAt + 1) - shiftedOffset;
+        if (!full || offset - skipped > curLength / 2) {
+            shiftRight(insertCnt, moveRightCnt);
+            int charsToSkip = Math.max(0, insertCnt - spareSpace);
+            finishAt = (finishAt + insertCnt) % value.length;
+            shiftedOffset = (shiftedOffset + insertCnt) % value.length;
+            full = curLength + insertCnt >= value.length;
+            insertStringTail(valToInsert, shiftedOffset, insertCnt);
+            skipped += charsToSkip;
+        }
+        else {
+            int moveLeftCnt = (curLength - moveRightCnt - insertCnt);
+            shiftLeft(insertCnt, moveLeftCnt);
+            insertStringTail(valToInsert, shiftedOffset, insertCnt);
+            skipped += valToInsert.length();
+        }
+    }
+
     /**
      * @return Count of skipped elements.
      */
     public int getSkipped() {
         return skipped;
     }
 
+    /**
+     * Returns a substring from the logical sequence of characters, accounting 
for
+     * the circular buffer structure and any skipped characters.
+     *
+     * <p>This method first validates the indices against the total logical 
length
+     * (skipped + visible characters). If the requested range is empty or 
fully within
+     * the skipped portion, an empty string is returned for efficiency.
+     *
+     * <p>It then calculates the physical indices in the internal array. If the
+     * substring wraps around the end of the circular buffer, it performs a 
two-part
+     * copy operation to assemble the result.
+     *
+     * @param beginIdx the beginning index, inclusive.
+     * @param endIdx the ending index, exclusive.
+     * @return a new String containing the specified subsequence.
+     * @throws StringIndexOutOfBoundsException if beginIdx or endIdx are 
negative,
+     *         or if endIdx is greater than the total logical length.
+     * @throws IllegalArgumentException if beginIdx is greater than endIdx.
+     */
+    public String substring(int beginIdx, int endIdx) {
+        if (beginIdx < 0 || endIdx < 0 || endIdx > skipped + length())
+            throw new StringIndexOutOfBoundsException(
+                    "Some of indexes is out of bounds. Begind index = " + 
beginIdx + " end index = " + endIdx);
+        if (beginIdx > endIdx)
+            throw new IllegalArgumentException(
+                    "Begin index can not be greater then end index (begin = " 
+ beginIdx + " end = " + endIdx + ")");

Review Comment:
   "Index out of bounds: beginIdx=" + beginIdx + ", endIdx=" + endIdx);



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to