Search code examples
objective-cunichar

unichar comparision in objective c


I need to implement a method, which compares two strings for equality, considering some turkish letters as latin(e.g. ı = i). That's bottleneck in program, so it needs to be implemented as efficient as possible.

I can't use NSString compare: withOption:nsdiactricinsensitivesearch, because it doesn't work with turkish letters correctly.

Here's the implementation of my algorithm:

- (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;
}

The problem is I can't compare unichars correctly. It doesn't compare correctly non-ascii symbols. How to deal with that?


Solution

  • Finally I found an answer.

    unichar is unsigned short, that means every symbol has its code. So we can compare them not as chars but as numbers.

    - (NSComparisonResult) compareTurkishSymbol:(unichar)ch with:(unichar)another
    {
        if (ch == 305) {//code of 'ı'
          ch = 'i';  
        }
        return ch - another;
    }