Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

why getNonControlCharacters did not take into account DoubleWidthCharacter? #202

Open
JinHeap opened this issue Jan 18, 2021 · 0 comments
Open

Comments

@JinHeap
Copy link

JinHeap commented Jan 18, 2021

public static String getNonControlCharacters(int maxChars, char[] buf, int offset, int charsLength) {
int len = Math.min(maxChars, charsLength);
final int origLen = len;
char tmp;
while (len > 0) {
tmp = buf[offset++];
if (0x20 <= tmp) { //stop when we reach control chars
len--;
continue;
}
offset--;
break;
}
int length = origLen - len;
return new String(buf, offset - length, length);
}

String nonControlCharacters = myDataStream.readNonControlCharacters(terminal.distanceToLineEnd());
terminal.writeCharacters(nonControlCharacters);

when input chinese simplified, the single character in chinese is doublewidthcharacter, then write one chinese char the X will plus 2, length be take as 2 also

terminal.writeCharacters(nonControlCharacters);

but before write will read chinese as length 1. it lead to something wrong.

Example A. chinese
myTeriminalWidth = 3.
the server return “简 体”, notice ,there is blank in middle, so the length is 5=2+1+2;
then will read "简 体“,because read did not consider as length 2, in fact I think it should read "间 ” 3=2+1.
image

Example B.english is ok
myTeriminalWidth = 3.
the server return “abcde”, the length is 5;
then will read "abc“.
image

as a result of
image

I think may can fix in twodifferent place

public String readNonControlCharacters(int maxChars) throws IOException {
String nonControlCharacters = CharUtils.getNonControlCharacters(maxChars, myBuf, myOffset, myLength);
myOffset += nonControlCharacters.length();
myLength -= nonControlCharacters.length();
return nonControlCharacters;
}

public static String getNonControlCharacters(int maxChars, char[] buf, int offset, int charsLength) {
int len = Math.min(maxChars, charsLength);
final int origLen = len;
char tmp;
while (len > 0) {
tmp = buf[offset++];
if (0x20 <= tmp) { //stop when we reach control chars
len--;
continue;
}
offset--;
break;
}
int length = origLen - len;
return new String(buf, offset - length, length);
}

my simple solution

 public static String getNonControlCharacters(int maxChars, char[] buf, int offset, int charsLength) {
        int len = Math.min(maxChars, charsLength);

        final int origLen = len;
        int countDoubleWidthCharacter = 0;
        char tmp;
        while (len > 0) {
            // remove comment
            //int codePoint = Character.codePointAt(buf, offset);
            //boolean doubleWidthCharacter = isDoubleWidthCharacter(codePoint, false);
            //if(doubleWidthCharacter){
            //    len --;
            //    countDoubleWidthCharacter++;
            //}
            tmp = buf[offset++];
            if (0x20 <= tmp) { //stop when we reach control chars
                len--;
                continue;
            }
            offset--;
            break;
        }

        int length = origLen - len;

        return new String(buf, offset - length + countDoubleWidthCharacter, length - countDoubleWidthCharacter);
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant