Created
September 9, 2024 00:47
-
-
Save pookjw/ac8fb592a4116a32c64394c7cf937f98 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
int main(int argc, const char * argv[]) { | |
@autoreleasepool { | |
const unsigned char string[] = "ABC😇123😅abc🥹"; | |
unsigned char tmp[4] = {'\0', '\0', '\0', '\0'}; | |
for (size_t idx = 0; idx < (sizeof(string) / sizeof(unsigned char)); idx++) { | |
const unsigned char _char = string[idx]; | |
if (_char == '\0') { | |
continue; | |
} else if (_char < 0b10000000) { | |
if (tmp[0] != '\0') { | |
NSLog(@"%@", [[NSString alloc] initWithBytes:tmp length:4 encoding:NSUTF8StringEncoding]); | |
} | |
for (size_t idx = 0; idx < 4; idx++) { | |
tmp[idx] = '\0'; | |
} | |
NSLog(@"%c", _char); | |
} else if (_char < 0b11000000) { | |
for (size_t idx = 0; idx < 4; idx++) { | |
if (tmp[idx] == '\0') { | |
if (idx == 0) { | |
abort(); | |
} else { | |
tmp[idx] = _char; | |
break; | |
} | |
} | |
} | |
} else if (_char >= 0b11000000) { | |
if (tmp[0] != '\0') { | |
NSLog(@"%@", [[NSString alloc] initWithBytes:tmp length:4 encoding:NSUTF8StringEncoding]); | |
} | |
tmp[0] = _char; | |
for (size_t idx = 1; idx < 4; idx++) { | |
tmp[idx] = '\0'; | |
} | |
} else { | |
abort(); | |
} | |
} | |
} | |
return 0; | |
} |
Author
pookjw
commented
Sep 9, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment