3

いくつかのトルコ語文字をラテン文字 (例: ı = i) と見なして、2 つの文字列が等しいかどうかを比較するメソッドを実装する必要があります。これはプログラムのボトルネックなので、できるだけ効率的に実装する必要があります。

NSString compare: withOption:nsdiactricinsensitivesearch を使用できません。トルコ語の文字を正しく処理できないためです。

私のアルゴリズムの実装は次のとおりです。

- (NSComparisonResult) compareTurkishSymbol:(unichar)ch with:(unichar)another
{
    //needs to be implemented
    //code like: if (ch == 'ı') doesn't work correctly
}

- (NSComparisonResult)compareTurkish:(NSString*)word with:(NSString*)another
{
    NSUInteger i;
    for (i =0; i < word.length; ++i) {
        NSComparisonResult result =[self compareTurkishSymbol:[word characterAtIndex:i] with:[another characterAtIndex:i]];
        if (result != NSOrderedSame) {
            return result;
        }
    }

    return another.length > word.length ? NSOrderedDescending : NSOrderedSame;
}

問題は、unichars を正しく比較できないことです。非ASCIIシンボルを正しく比較しません。それに対処する方法は?

4

1 に答える 1

3

最後に答えを見つけました。

unichar は unsigned short です。つまり、すべてのシンボルにコードがあります。したがって、それらを文字ではなく数値として比較できます。

- (NSComparisonResult) compareTurkishSymbol:(unichar)ch with:(unichar)another
{
    if (ch == 305) {//code of 'ı'
      ch = 'i';  
    }
    return ch - another;
}
于 2012-02-16T10:16:49.743 に答える