i made this function because GetClientName doesnt cuts string in utf8 safe way
Code:
new const UTF8MULTIBYTECHAR = (1 << 7);
//this function assumes string is a correctly encoded utf8 string that is cutted in not utf8 safe way.
stock terminateUTF8String(String:buffer[], const maxlength = -1){
if(maxlength > 0){
buffer[maxlength - 1] = '\0';
}
new length = strlen(buffer);
new bytescounted = 0;
if(length <= 0){
return 0;
}
for(new i = length - 1; i >= 0; i--){
if(UTF8MULTIBYTECHAR & buffer[i] == '\0'){
return 0;//its a single byte character, we have nothing to do.
}else{
//j is not a good idea...
for(new j = 1; j <= 7; j++){
if((UTF8MULTIBYTECHAR >> j) & buffer[i] == '\0'){
if(j == 1){
//its part of multi byte character
bytescounted++;
break;
}else{
//its starting byte of multi byte character, so lets see if we readed enough amount of utf8 strings before and cut it if its not.
if(bytescounted != (j - 1)){
buffer[i] = '\0';
}
return 0;
}
}
}
}
}
return 0;
}
changed code a bit and tested and working.
__________________