Created
August 24, 2016 12:30
-
-
Save hoshi-takanori/9db1934b8207def0d3364f9d642de3c3 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> | |
const char *kindForString(NSString *str) | |
{ | |
if (str.length > 1) { | |
return "surrogate"; | |
} | |
if ([@"0123456789" containsString:str]) { | |
return "digit"; | |
} | |
if ([@"ABCDEFGHIJKLMNOPQRSTUVWXYZ" containsString:str]) { | |
return "upper"; | |
} | |
if ([@"abcdefghijklmnopqrstuvwxyz" containsString:str]) { | |
return "lower"; | |
} | |
if ([str canBeConvertedToEncoding:NSASCIIStringEncoding]) { | |
return "ascii"; | |
} | |
if ([str canBeConvertedToEncoding:NSISOLatin1StringEncoding]) { | |
return "latin1"; | |
} | |
int c = [str characterAtIndex:0]; | |
if (c >= 0x3040 && c <= 0x309f) { | |
return "hiragana"; | |
} | |
if (c >= 0x30a0 && c <= 0x30ff) { | |
return "katagana"; | |
} | |
if (c >= 0x4e00 && c <= 0x9fbf) { | |
return "kanji"; | |
} | |
return "other"; | |
} | |
void processData(NSData *data) | |
{ | |
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; | |
//NSLog(@"str = %@", str); | |
NSMutableDictionary *dict = [NSMutableDictionary new]; | |
for (int i = 0; i < str.length; i++) { | |
NSString *key; | |
if (i + 1 < str.length && | |
CFStringIsSurrogateHighCharacter([str characterAtIndex:i]) && | |
CFStringIsSurrogateLowCharacter([str characterAtIndex:i + 1])) { | |
key = [str substringWithRange:NSMakeRange(i, 2)]; | |
i++; | |
} else { | |
key = [str substringWithRange:NSMakeRange(i, 1)]; | |
} | |
if ([key isEqualToString:@"\n"] || [key isEqualToString:@"\r"]) { | |
continue; | |
} | |
int cnt = [[dict objectForKey:key] intValue]; | |
[dict setObject:[NSNumber numberWithInt:cnt + 1] forKey:key]; | |
} | |
//NSLog(@"dict = %@", dict); | |
for (NSString *key in [dict allKeys]) { | |
int cnt = [[dict objectForKey:key] intValue]; | |
const char *kind = kindForString(key); | |
printf("%s\t%d\t%s\n", [key UTF8String], cnt, kind); | |
} | |
} | |
int main(int argc, char *argv[]) | |
{ | |
@autoreleasepool { | |
if (argc == 1) { | |
NSFileHandle *stdin = [NSFileHandle fileHandleWithStandardInput]; | |
processData([stdin availableData]); | |
} else { | |
NSString *filename = [[NSString alloc] initWithUTF8String:argv[1]]; | |
processData([[NSData alloc] initWithContentsOfFile:filename]); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment