cdts/xdts-ios 3/TreeHole/Code/Utility/TextInputLimit/NSString+MTLimit.m

131 lines
5.0 KiB
Mathematica
Raw Normal View History

2023-07-27 09:20:00 +08:00
//
// NSString+MTLimit.m
// Meet
//
// Created by ko1o on 2018/10/22.
// Copyright © 2018 ko1o. All rights reserved.
//
#import "NSString+MTLimit.h"
@implementation NSString (MTLimit)
- (NSInteger)mt_customByteLength
{
//strlength..1
NSUInteger len = self.length;
//
// NSString* pattern = @"[\u4e00-\u9fa5]";
// NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
// //
// NSInteger numMatch = [regex numberOfMatchesInString:self options:NSMatchingReportProgress range:NSMakeRange(0, len)];
// //21
// return len + numMatch;
//1,1,emoj 2-7
static NSRegularExpression* regex;
if (!regex) {
NSString* pattern = @"[a-zA-Z]";
regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
}
NSInteger numMatch = [regex numberOfMatchesInString:self options:NSMatchingReportProgress range:NSMakeRange(0, len)];
return len - (numMatch ) / 2;
}
- (NSString *)mt_safeSubStringWithStringLength:(NSInteger)maxLength
{
return [self mt_safeSubStringWithByteLength:maxLength isCaculateCustomByteLength:NO];
}
- (NSString *)mt_safeSubStringWithByteLength:(NSInteger)maxLength
{
return [self mt_safeSubStringWithByteLength:maxLength isCaculateCustomByteLength:YES];
}
- (NSString *)mt_safeSubStringWithByteLength:(NSInteger)maxLength isCaculateCustomByteLength:(BOOL)isCaculateCustomByteLength
{
NSString * textString = self;
NSRange fullRange = NSMakeRange(0, [textString length]);
__block NSMutableString * resultString = [NSMutableString string];
NSInteger currentLength = isCaculateCustomByteLength ? textString.mt_customByteLength :textString.length;
if (currentLength > maxLength) {
[textString enumerateSubstringsInRange:fullRange
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop)
{
// NSLog(@"%@ %@", substring, NSStringFromRange(substringRange));
NSMutableString * toString = [resultString mutableCopy];
[toString appendString:substring];
NSInteger currentLength = isCaculateCustomByteLength ? toString.mt_customByteLength :toString.length;
if (currentLength <= maxLength) {
resultString = toString;
} else {
*stop = YES;
}
}];
textString = [resultString copy];
}
return textString;
}
// Unicode4
- (NSInteger)mt_unicodeCharacterByteLength
{
NSString * tempStr = self;
int strlength = 0;
char* p = (char*)[tempStr cStringUsingEncoding:NSUnicodeStringEncoding];
for (int i=0 ; i<[tempStr lengthOfBytesUsingEncoding:NSUnicodeStringEncoding] ;i++) {
if (*p) {
p++;
strlength++;
}
else {
p++;
}
}
return strlength;
}
- (BOOL)mt_stringContainsEmoji
{
// returnValueNOYES
__block BOOL returnValue = NO;
[self enumerateSubstringsInRange:NSMakeRange(0, [self length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
const unichar hs = [substring characterAtIndex:0];
// surrogate pair
if (0xd800 <= hs && hs <= 0xdbff) {
if (substring.length > 1) {
const unichar ls = [substring characterAtIndex:1];
const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
if (0x1d000 <= uc && uc <= 0x1f77f) {
returnValue = YES;
}
}
} else if (substring.length > 1) {
const unichar ls = [substring characterAtIndex:1];
if (ls == 0x20e3) {
returnValue = YES;
}
} else {
// non surrogate
if (0x2100 <= hs && hs <= 0x27ff) {
returnValue = YES;
} else if (0x2B05 <= hs && hs <= 0x2b07) {
returnValue = YES;
} else if (0x2934 <= hs && hs <= 0x2935) {
returnValue = YES;
} else if (0x3297 <= hs && hs <= 0x3299) {
returnValue = YES;
} else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
returnValue = YES;
}
}
}];
return returnValue;
}
@end