Skip to content

Instantly share code, notes, and snippets.

@pookjw
Created September 9, 2024 00:47
Show Gist options
  • Save pookjw/ac8fb592a4116a32c64394c7cf937f98 to your computer and use it in GitHub Desktop.
Save pookjw/ac8fb592a4116a32c64394c7cf937f98 to your computer and use it in GitHub Desktop.
#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;
}
@pookjw
Copy link
Author

pookjw commented Sep 9, 2024

A
B
C
😇
1
2
3
😅
a
b
c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment