Raised This Month: $ Target: $400
 0% 

[stock]utf8 safe string cutter


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Author Message
javalia
Senior Member
Join Date: May 2009
Location: korea, republic of
Old 01-11-2013 , 14:53   [stock]utf8 safe string cutter
Reply With Quote #1

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

Last edited by javalia; 01-12-2013 at 06:11.
javalia is offline
 



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 13:42.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode