// // WordUtils.m // UUTUtils // // Created by zhuge on 2017/12/26. // Copyright © 2017年 zhuge. All rights reserved. // #import "WordUtils.h" NSString *letters = @"abcdefghijklmnopqrstuvwxyz"; @interface WordUtils() @property (strong) NSArray *arrKeywords; @end @implementation WordUtils - (instancetype)init { self = [super init]; if (self) { [self reset]; NSString *path = [[NSBundle mainBundle] pathForResource:@"CY_String_keywords" ofType:@"txt"]; NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; self.arrKeywords = [str componentsSeparatedByString:@","]; } return self; } - (void)reset { self.iCurrentIndex = 1; self.maUsedKeywords = [NSMutableArray arrayWithCapacity:0]; [self.maUsedKeywords addObject:@"abstract"]; [self.maUsedKeywords addObject:@"assert"]; [self.maUsedKeywords addObject:@"boolean"]; [self.maUsedKeywords addObject:@"break"]; [self.maUsedKeywords addObject:@"byte"]; [self.maUsedKeywords addObject:@"case"]; [self.maUsedKeywords addObject:@"catch"]; [self.maUsedKeywords addObject:@"char"]; [self.maUsedKeywords addObject:@"class"]; [self.maUsedKeywords addObject:@"const"]; [self.maUsedKeywords addObject:@"continue"]; [self.maUsedKeywords addObject:@"default"]; [self.maUsedKeywords addObject:@"do"]; [self.maUsedKeywords addObject:@"double"]; [self.maUsedKeywords addObject:@"else"]; [self.maUsedKeywords addObject:@"enum"]; [self.maUsedKeywords addObject:@"extends"]; [self.maUsedKeywords addObject:@"final"]; [self.maUsedKeywords addObject:@"finally"]; [self.maUsedKeywords addObject:@"float"]; [self.maUsedKeywords addObject:@"for"]; [self.maUsedKeywords addObject:@"goto"]; [self.maUsedKeywords addObject:@"if"]; [self.maUsedKeywords addObject:@"implements"]; [self.maUsedKeywords addObject:@"import"]; [self.maUsedKeywords addObject:@"instanceof"]; [self.maUsedKeywords addObject:@"int"]; [self.maUsedKeywords addObject:@"interface"]; [self.maUsedKeywords addObject:@"long"]; [self.maUsedKeywords addObject:@"native"]; [self.maUsedKeywords addObject:@"new"]; [self.maUsedKeywords addObject:@"package"]; [self.maUsedKeywords addObject:@"private"]; [self.maUsedKeywords addObject:@"protected"]; [self.maUsedKeywords addObject:@"public"]; [self.maUsedKeywords addObject:@"return"]; [self.maUsedKeywords addObject:@"strictfp"]; [self.maUsedKeywords addObject:@"short"]; [self.maUsedKeywords addObject:@"static"]; [self.maUsedKeywords addObject:@"super"]; [self.maUsedKeywords addObject:@"switch"]; [self.maUsedKeywords addObject:@"synchronized"]; [self.maUsedKeywords addObject:@"this"]; [self.maUsedKeywords addObject:@"throw"]; [self.maUsedKeywords addObject:@"throws"]; [self.maUsedKeywords addObject:@"transient"]; [self.maUsedKeywords addObject:@"try"]; [self.maUsedKeywords addObject:@"void"]; [self.maUsedKeywords addObject:@"volatile"]; [self.maUsedKeywords addObject:@"while"]; [self.maUsedKeywords addObject:@"true"]; [self.maUsedKeywords addObject:@"false"]; } - (NSString *)nextStringFor26Hex { NSString *ret = @""; int n = _iCurrentIndex; while (n > 0) { int m = n % 26; if (m == 0) { m = 26; } n = (n - m) / 26; ret = [NSString stringWithFormat:@"%c%@", (char)(m + 'a' - 1), ret]; } self.iCurrentIndex++; return ret; } - (NSString *)nextDistinctKeyword { int notFoundTimes = 0; NSString *str = _arrKeywords[arc4random() % [_arrKeywords count]]; while ([_maUsedKeywords containsObject:str]) { // NSLog(@"----------------------repeated"); notFoundTimes++; if (notFoundTimes > 10) { str = [NSString stringWithFormat:@"%@_%@", _arrKeywords[arc4random() % [_arrKeywords count]], _arrKeywords[arc4random() % [_arrKeywords count]]]; } else { str = _arrKeywords[arc4random() % [_arrKeywords count]]; } } str = [str lowercaseString]; [_maUsedKeywords addObject:str]; return str; } #pragma mark static +(NSString*) randomStringWithLength:(int) len { NSMutableString *randomString = [NSMutableString stringWithCapacity:len]; for (int i = 0; i < len; i++) { [randomString appendFormat:@"%C", [letters characterAtIndex:arc4random() % [letters length]]]; } return randomString; } +(NSString *)randomKeyword { NSString *path = [[NSBundle mainBundle] pathForResource:@"CY_String_keywords" ofType:@"txt"]; NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; NSArray *arr = [str componentsSeparatedByString:@","]; NSString *keyword = [arr objectAtIndex:arc4random() % [arr count]]; return keyword; } + (NSString *)reverse:(NSString *)str { NSMutableString *reversedString = [NSMutableString stringWithCapacity:[str length]]; [str enumerateSubstringsInRange:NSMakeRange(0,[str length]) options:(NSStringEnumerationReverse | NSStringEnumerationByComposedCharacterSequences) usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) { [reversedString appendString:substring]; }]; return reversedString; } + (NSString *)urlEncode:(NSString *)str { // NSString *encoded = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes( // NULL, // (CFStringRef)str, // NULL, // (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ", // kCFStringEncodingUTF8 )); NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"!*'\"();:@&=+$,/?%#[]% "]; NSString *encoded = [str stringByAddingPercentEncodingWithAllowedCharacters:set]; return encoded; } + (NSString *)stringByTrimHTML:(NSString *)str { str = [str stringByReplacingOccurrencesOfString:@"
" withString:@"
"]; str = [str stringByReplacingOccurrencesOfString:@"
" withString:@"\n"]; NSRange r; NSString *s = [str copy]; while ((r = [s rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound) s = [s stringByReplacingCharactersInRange:r withString:@""]; return s; } + (NSString *)trimSpaceOfString:(NSString *)str { return [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; } + (NSString *)removeAllSuffix:(NSString *)suffix ofString:(NSString *)string { NSString *ret = [NSString stringWithString:string]; while ([ret hasSuffix:suffix]) { ret = [ret substringToIndex:[ret length] - [suffix length]]; } return ret; } + (NSString *)removeAllPrefix:(NSString *)prefix ofString:(NSString *)string { NSString *ret = [NSString stringWithString:string]; while ([ret hasPrefix:prefix]) { ret = [ret substringFromIndex:[prefix length]]; } return ret; } @end