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


##########
java/debugger.jpda/src/org/netbeans/modules/debugger/jpda/models/ShortenedStrings.java:
##########
@@ -385,19 +349,220 @@ 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,
+            InternalStringEncoding encoding, boolean isLittleEndian) throws
+            InternalExceptionWrapper, ObjectCollectedExceptionWrapper,
+            VMDisconnectedExceptionWrapper{
+        if (encoding == InternalStringEncoding.CHAR_ARRAY){
+            //that was easy
+            Value v = ArrayReferenceWrapper.getValue(sourceArray, index);
+            if (!(v instanceof CharValue)){
+                return -1;
+            }
+            return ((CharValue)v).charValue();
+        }
+        if (encoding == InternalStringEncoding.BYTE_ARRAY_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
+        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(), isLittleEndian);
+    }
+    /**

Review Comment:
   Please also make sure, that there is an empty line between the end of the 
previous method and the comment.



##########
java/debugger.jpda/src/org/netbeans/modules/debugger/jpda/models/ShortenedStrings.java:
##########
@@ -385,19 +349,220 @@ 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,
+            InternalStringEncoding encoding, boolean isLittleEndian) throws
+            InternalExceptionWrapper, ObjectCollectedExceptionWrapper,
+            VMDisconnectedExceptionWrapper{
+        if (encoding == InternalStringEncoding.CHAR_ARRAY){
+            //that was easy
+            Value v = ArrayReferenceWrapper.getValue(sourceArray, index);
+            if (!(v instanceof CharValue)){
+                return -1;
+            }
+            return ((CharValue)v).charValue();
+        }
+        if (encoding == InternalStringEncoding.BYTE_ARRAY_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
+        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(), isLittleEndian);
+    }
+    /**
+     * (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 encoding
+     * @param isLittleEndian
+     * @return
+     */
+    private static int charAt(List<Value> sourceArray, int index,
+            InternalStringEncoding encoding, boolean isLittleEndian){
+        if (encoding == InternalStringEncoding.CHAR_ARRAY){
+            //that was easy
+            Value v = sourceArray.get(index);
+            if (!(v instanceof CharValue)){
+                return -1;
+            }
+            return ((CharValue)v).charValue();
+        }
+        if (encoding == InternalStringEncoding.BYTE_ARRAY_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
+        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(), 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 encoding
+     * @param isLittleEndian
+     * @param start
+     * @param length
+     * @param dest
+     */
+    private static void copyToCharArray(ArrayReference sourceArray,
+            int srcPos, char[] dest, int destPos, int length,
+            InternalStringEncoding encoding, boolean isLittleEndian) throws
+            ObjectCollectedExceptionWrapper, VMDisconnectedExceptionWrapper,
+            InternalExceptionWrapper, IOException{
+        //grab applicable values
+        int realStart = srcPos;
+        int realLength = length;
+        if (encoding == InternalStringEncoding.BYTE_ARRAY_UTF16){
+            realStart *= 2;
+            realLength *= 2;
+        }
+        List<Value> values = ArrayReferenceWrapper.getValues(sourceArray,
+                realStart, realLength);
+        //copy them
+        copyToCharArray(values, 0, dest, destPos, length, encoding,
+                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
+     */
+    private static void copyToCharArray(List<Value> sourceArray, int srcPos,
+            char[] dest, int destPos, int length, InternalStringEncoding 
backing,
+            boolean isLittleEndian) throws IOException{
+        if (backing == InternalStringEncoding.CHAR_ARRAY){
+            //that was easy
+            for (int i = 0; i < length; i++) {
+                Value v = sourceArray.get(i + srcPos);
+                if (!(v instanceof CharValue)){
+                    throw new IOException(MessageFormat.format("Char at {0} "
+                            + "is not a character: {1}", srcPos + i, v));
+                }
+                dest[destPos + i] = ((CharValue)v).charValue();
+            }
+            return;
+        }
+        if (backing == InternalStringEncoding.BYTE_ARRAY_LATIN1){
+            //that was also easy
+            for (int i = 0; i < length; i++) {
+                Value v = sourceArray.get(i + srcPos);
+                if (!(v instanceof ByteValue)){
+                    throw new IOException(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;
+        }
+        //uft16 it is
+        assert backing == InternalStringEncoding.BYTE_ARRAY_UTF16;
+        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)){
+                throw new IOException(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(), isLittleEndian);
+        }
+    }
+    private static char utf16Combine(byte left, byte right, boolean 
isLittleEndian){
+        int hiByteShift, lowByteShift;
+        if (isLittleEndian){
+            hiByteShift = 0;
+            lowByteShift = 8;
+        }
+        else{
+            hiByteShift = 8;
+            lowByteShift = 0;
+        }
+        char c1 = (char)left;
+        char c2 = (char)right;
+        //remove the extended sign
+        c1 = (char) (0xFF & c1);
+        c2 = (char) (0xFF & c2);
+        char c = (char)(c1 << hiByteShift |
+                c2 << lowByteShift);
+        return c;
+    }
+    private static int length(int arrayLength, InternalStringEncoding 
backingEncoding){

Review Comment:
   Please also make sure, that there is an empty line between the end of the 
previous method and the next one.



##########
java/debugger.jpda/src/org/netbeans/modules/debugger/jpda/models/ShortenedStrings.java:
##########
@@ -385,19 +349,220 @@ 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,
+            InternalStringEncoding encoding, boolean isLittleEndian) throws
+            InternalExceptionWrapper, ObjectCollectedExceptionWrapper,
+            VMDisconnectedExceptionWrapper{
+        if (encoding == InternalStringEncoding.CHAR_ARRAY){
+            //that was easy
+            Value v = ArrayReferenceWrapper.getValue(sourceArray, index);
+            if (!(v instanceof CharValue)){
+                return -1;
+            }
+            return ((CharValue)v).charValue();
+        }
+        if (encoding == InternalStringEncoding.BYTE_ARRAY_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
+        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(), isLittleEndian);
+    }
+    /**
+     * (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 encoding
+     * @param isLittleEndian
+     * @return
+     */
+    private static int charAt(List<Value> sourceArray, int index,
+            InternalStringEncoding encoding, boolean isLittleEndian){
+        if (encoding == InternalStringEncoding.CHAR_ARRAY){
+            //that was easy
+            Value v = sourceArray.get(index);
+            if (!(v instanceof CharValue)){
+                return -1;
+            }
+            return ((CharValue)v).charValue();
+        }
+        if (encoding == InternalStringEncoding.BYTE_ARRAY_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
+        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(), 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 encoding
+     * @param isLittleEndian
+     * @param start
+     * @param length
+     * @param dest
+     */
+    private static void copyToCharArray(ArrayReference sourceArray,
+            int srcPos, char[] dest, int destPos, int length,
+            InternalStringEncoding encoding, boolean isLittleEndian) throws
+            ObjectCollectedExceptionWrapper, VMDisconnectedExceptionWrapper,
+            InternalExceptionWrapper, IOException{
+        //grab applicable values
+        int realStart = srcPos;
+        int realLength = length;
+        if (encoding == InternalStringEncoding.BYTE_ARRAY_UTF16){
+            realStart *= 2;
+            realLength *= 2;
+        }
+        List<Value> values = ArrayReferenceWrapper.getValues(sourceArray,
+                realStart, realLength);
+        //copy them
+        copyToCharArray(values, 0, dest, destPos, length, encoding,
+                isLittleEndian);
+    }
+    /**

Review Comment:
   Please also make sure, that there is an empty line between the end of the 
previous method and the comment.



##########
java/debugger.jpda/src/org/netbeans/modules/debugger/jpda/models/ShortenedStrings.java:
##########
@@ -472,4 +630,20 @@ private static class StringValueInfo {
             this.shortValueRef = new WeakReference<String>(shortenedValue);
         }
     }
+    private enum InternalStringEncoding {

Review Comment:
   Please also make sure, that there is an empty line between the end of the 
previous method and the enum declaration.
   



##########
java/debugger.jpda/src/org/netbeans/modules/debugger/jpda/models/ShortenedStrings.java:
##########
@@ -385,19 +349,220 @@ else if (valuesField.type() instanceof ArrayType &&
         }
         return string;
     }
+    //would return char, but exceptions are expensive and so is boxing into

Review Comment:
   Please move this line comment into the javadoc, if you want make it an 
implementation note, but there is no need for two comments here. Please also 
make sure, that there is an empty line between the end of the previous method 
and the comment.



##########
java/debugger.jpda/src/org/netbeans/modules/debugger/jpda/models/ShortenedStrings.java:
##########
@@ -385,19 +349,220 @@ 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,
+            InternalStringEncoding encoding, boolean isLittleEndian) throws
+            InternalExceptionWrapper, ObjectCollectedExceptionWrapper,
+            VMDisconnectedExceptionWrapper{
+        if (encoding == InternalStringEncoding.CHAR_ARRAY){
+            //that was easy
+            Value v = ArrayReferenceWrapper.getValue(sourceArray, index);
+            if (!(v instanceof CharValue)){
+                return -1;
+            }
+            return ((CharValue)v).charValue();
+        }
+        if (encoding == InternalStringEncoding.BYTE_ARRAY_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
+        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(), isLittleEndian);
+    }
+    /**
+     * (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 encoding
+     * @param isLittleEndian
+     * @return
+     */
+    private static int charAt(List<Value> sourceArray, int index,
+            InternalStringEncoding encoding, boolean isLittleEndian){
+        if (encoding == InternalStringEncoding.CHAR_ARRAY){
+            //that was easy
+            Value v = sourceArray.get(index);
+            if (!(v instanceof CharValue)){
+                return -1;
+            }
+            return ((CharValue)v).charValue();
+        }
+        if (encoding == InternalStringEncoding.BYTE_ARRAY_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
+        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(), isLittleEndian);
+    }
+    /**

Review Comment:
   Please also make sure, that there is an empty line between the end of the 
previous method and the comment.



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