SirIntellegence commented on code in PR #6157:
URL: https://github.com/apache/netbeans/pull/6157#discussion_r1258584147


##########
java/debugger.jpda/src/org/netbeans/modules/debugger/jpda/models/ShortenedStrings.java:
##########
@@ -385,19 +348,240 @@ else if (valuesField.type() instanceof ArrayType &&
         }
         return string;
     }
+    //would return char, but exceptions are expensive and so is boxing into
+    //a Character
+    /**
+     * (Currently untested) Grab the char at the given index in the array.
+     * Returns -1 on an error
+     * @param sourceArray Backing array reference. May be a byte or char array
+     * @param index
+     * @param backing
+     * @param isLittleEndian
+     * @return
+     */
+    private static int charAt(ArrayReference sourceArray, int index,
+            Utf16State backing, boolean isLittleEndian) throws
+            InternalExceptionWrapper, ObjectCollectedExceptionWrapper,
+            VMDisconnectedExceptionWrapper{
+        if (backing == Utf16State.Chars){
+            //that was easy
+            Value v = ArrayReferenceWrapper.getValue(sourceArray, index);
+            if (!(v instanceof CharValue)){
+                return -1;
+            }
+            return ((CharValue)v).charValue();
+        }
+        if (backing == Utf16State.Latin1){
+            //that was also easy
+            Value v = ArrayReferenceWrapper.getValue(sourceArray, index);
+            if (!(v instanceof ByteValue)){
+                return -1;
+            }
+            char c = (char)((ByteValue)v).byteValue();
+            //strip off the sign value
+            c &= 0xff;
+            return c;
+        }
+        //uft16 it is
+        int hiByteShift, lowByteShift;
+        if (isLittleEndian){
+            hiByteShift = 0;
+            lowByteShift = 8;
+        }
+        else{
+            hiByteShift = 8;
+            lowByteShift = 0;
+        }
+        List<Value> vals = ArrayReferenceWrapper.getValues(sourceArray,
+                index * 2, 2);
+        Value left = vals.get(0);
+        Value right = vals.get(1);
+        if (!(left instanceof ByteValue && right instanceof ByteValue)){
+            return -1;
+        }
+        return utf16Combine(((ByteValue)left).byteValue(),
+                ((ByteValue)right).value(), hiByteShift, lowByteShift);
+    }
+    /**
+     * (Currently untested) Grab the char at the given index in the array.
+     * Returns -1 on an error
+     * @param sourceArray Backing array reference. May be a byte or char array
+     * @param index
+     * @param backing
+     * @param isLittleEndian
+     * @return
+     */
+    private static int charAt(List<Value> sourceArray, int index,
+            Utf16State backing, boolean isLittleEndian){
+        if (backing == Utf16State.Chars){
+            //that was easy
+            Value v = sourceArray.get(index);
+            if (!(v instanceof CharValue)){
+                return -1;
+            }
+            return ((CharValue)v).charValue();
+        }
+        if (backing == Utf16State.Latin1){
+            //that was also easy
+            Value v = sourceArray.get(index);
+            if (!(v instanceof ByteValue)){
+                return -1;
+            }
+            char c = (char)((ByteValue)v).byteValue();
+            //strip off the sign value
+            c &= 0xff;
+            return c;
+        }
+        //uft16 it is
+        int hiByteShift, lowByteShift;
+        if (isLittleEndian){
+            hiByteShift = 0;
+            lowByteShift = 8;
+        }
+        else{
+            hiByteShift = 8;
+            lowByteShift = 0;
+        }
+        Value left = sourceArray.get(index * 2);
+        Value right = sourceArray.get((index * 2) + 1);
+        if (!(left instanceof ByteValue && right instanceof ByteValue)){
+            return -1;
+        }
+        return utf16Combine(((ByteValue)left).byteValue(),
+                ((ByteValue)right).value(), hiByteShift, lowByteShift);
+    }
+    /**
+     * Copy the input to the destination array as if by {@link 
System#arrayCopy}
+     * @param sourceArray Backing array reference. May be a byte or char array
+     * @param backing
+     * @param isLittleEndian
+     * @param start
+     * @param length
+     * @param dest
+     * @return Null on success, otherwise an error message
+     */
+    private static String copyToCharArray(ArrayReference sourceArray,
+            int srcPos, char[] dest, int destPos, int length,
+            Utf16State backing, boolean isLittleEndian) throws
+            ObjectCollectedExceptionWrapper, VMDisconnectedExceptionWrapper,
+            InternalExceptionWrapper{
+        //grab applicable values
+        int realStart = srcPos;
+        int realLength = length;
+        if (backing == Utf16State.Utf16){
+            realStart *= 2;
+            realLength *= 2;
+        }
+        List<Value> values = ArrayReferenceWrapper.getValues(sourceArray,
+                realStart, realLength);
+        //copy them
+        return copyToCharArray(values, 0, dest, destPos, length, backing,
+                isLittleEndian);
+    }
+    /**
+     * Copy the input to the destination array as if by {@link 
System#arrayCopy}
+     * @param sourceArray Backing array reference. May be a byte or char array
+     * @param backing
+     * @param isLittleEndian
+     * @param start
+     * @param length
+     * @param dest
+     * @return Null on success, otherwise an error message
+     */
+    private static String copyToCharArray(List<Value> sourceArray, int srcPos,
+            char[] dest, int destPos, int length, Utf16State backing,
+            boolean isLittleEndian){
+        if (backing == Utf16State.Chars){
+            //that was easy
+            for (int i = 0; i < length; i++) {
+                Value v = sourceArray.get(i + srcPos);
+                if (!(v instanceof CharValue)){
+                    return MessageFormat.format("Char at {0} is not a 
character: {1}", srcPos + i, v);
+                }
+                dest[destPos + i] = ((CharValue)v).charValue();
+            }
+            return null;
+        }
+        if (backing == Utf16State.Latin1){
+            //that was also easy
+            for (int i = 0; i < length; i++) {
+                Value v = sourceArray.get(i + srcPos);
+                if (!(v instanceof ByteValue)){
+                    return MessageFormat.format("Char at {0} is not a byte: 
{1}", srcPos + i, v);
+                }
+                char c = (char)((ByteValue)v).byteValue();
+                //strip off the sign value
+                c &= 0xff;
+                dest[destPos + i] = c;
+            }
+            return null;
+        }
+        //uft16 it is
+        assert backing == Utf16State.Utf16;
+        int hiByteShift, lowByteShift;
+        if (isLittleEndian){
+            hiByteShift = 0;
+            lowByteShift = 8;
+        }
+        else{
+            hiByteShift = 8;
+            lowByteShift = 0;
+        }
+        for (int i = 0; i < length; i++) {
+            Value left = sourceArray.get(i * 2);
+            Value right = sourceArray.get((i * 2) + 1);
+            if (!(left instanceof ByteValue && right instanceof ByteValue)){
+                return MessageFormat.format("Char at {0} is not a byte pair: "
+                        + "{1},{2}", srcPos + i, left, right);
+            }
+            dest[destPos + i] = utf16Combine(((ByteValue)left).byteValue(),
+                    ((ByteValue)right).byteValue(), hiByteShift, lowByteShift);
+        }
+        return null;
+    }
+    private static char utf16Combine(byte left, byte right, int hiByteShift,

Review Comment:
   While I agree with this, I wanted to cut down on branching... That may not 
be necessary though? I have a tendency to try to optimize wherever I can when 
it is trivial to do so. Do you want me to still make that change?
   ...
   After thinking for a moment, I think I will do that. Readability is more 
important than micro-optimizations



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to