0

I have a function written in C#, i want to convert it to objective-c. How to do it?

public static string UnicodeUnSign(string s)
{
    const string uniChars = "àáảãạâầấẩẫậăằắẳẵặèéẻẽẹêềếểễệđìíỉĩịòóỏõọôồốổỗộơờớởỡợùúủũụưừứửữựỳýỷỹỵÀÁẢÃẠÂẦẤẨẪẬĂẰẮẲẴẶÈÉẺẼẸÊỀẾỂỄỆĐÌÍỈĨỊÒÓỎÕỌÔỒỐỔỖỘƠỜỚỞỠỢÙÚỦŨỤƯỪỨỬỮỰỲÝỶỸỴÂĂĐÔƠƯ";
    const string koDauChars = "aaaaaaaaaaaaaaaaaeeeeeeeeeeediiiiiooooooooooooooooouuuuuuuuuuuyyyyyAAAAAAAAAAAAAAAAAEEEEEEEEEEEDIIIOOOOOOOOOOOOOOOOOOOUUUUUUUUUUUYYYYYAADOOU";

    if (string.IsNullOrEmpty(s))
    {
        return s;
    }

    string retVal = String.Empty;
    for (int i = 0; i < s.Length; i++)
    {
        int pos = uniChars.IndexOf(s[i].ToString());
        if (pos >= 0)
            retVal += koDauChars[pos];
        else
            retVal += s[i];
    }
    return retVal;
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
BlueSky
  • 139
  • 3
  • 9
  • 5
    I imagine the first step would be to learn Objective-C – borrrden Mar 01 '13 at 09:00
  • Personally, I have very good experiences using the [Xamarin stack of tools](http://xamarin.com/) to write C# apps for iOS and OS X. – Uwe Keim Mar 01 '13 at 09:05
  • No,you don't understand, i want to convert the function delete sign above, i don't want to create app native IOS from C#. – BlueSky Mar 01 '13 at 09:14

3 Answers3

2

Without resorting to core foundation:

#import <Foundation/Foundation.h>

int main (int argc, const char *argv[]) {
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

   NSString *unicodeCharacters = @"àáảãạâầấẩẫậăằắẳẵặèéẻẽẹêềếểễệđìíỉĩịòóỏõọôồốổỗộơờớởỡợùúủũụưừứửữựỳýỷỹỵÀÁẢÃẠÂẦẤẨẪẬĂẰẮẲẴẶÈÉẺẼẸÊỀẾỂỄỆĐÌÍỈĨỊÒÓỎÕỌÔỒỐỔỖỘƠỜỚỞỠỢÙÚỦŨỤƯỪỨỬỮỰỲÝỶỸỴÂĂĐÔƠƯ";

   NSString *decomposed = [unicodeCharacters decomposedStringWithCanonicalMapping];
   NSLocale *usLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];

   NSString *cleaned = [decomposed stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:usLocale];
   cleaned = [cleaned stringByReplacingOccurrencesOfString:@"đ" withString:@"d"];
   cleaned = [cleaned stringByReplacingOccurrencesOfString:@"Đ" withString:@"D"];
   NSLog (@"%@", cleaned);

   [pool drain];
   return 0;
}
Cameron Lowell Palmer
  • 21,528
  • 7
  • 125
  • 126
  • 1
    Good answer! I know about `decomposedStringWithCanonicalMapping` but not about `stringByFoldingWithOptions`. - Is it necessary to decompose the string first? At least with the input string from this question is seems not to make a difference. – Martin R Mar 09 '13 at 09:12
  • Largely as a safety precaution. It all depends on the source of a string and if you want the text to be consistent. According to the QA article at Apple it probably doesn't matter as long as you're on a Mac exclusively. http://developer.apple.com/library/mac/#qa/qa1235/_index.html – Cameron Lowell Palmer Mar 10 '13 at 11:08
1

You could use the CoreFoundation CFStringTransform function which does almost all transformations from your list. Only "đ" and "Đ" have to be handled separately:

NSString *UnicodeUnsign(NSString *s)
{
    NSMutableString *result = [s mutableCopy];
    // __bridge only required if you compile with ARC:
    CFStringTransform((__bridge CFMutableStringRef)result, NULL, kCFStringTransformStripCombiningMarks, NO);

    [result replaceOccurrencesOfString:@"đ" withString:@"d" options:0 range:NSMakeRange(0, [result length])];
    [result replaceOccurrencesOfString:@"Đ" withString:@"D" options:0 range:NSMakeRange(0, [result length])];

    return result;
}

Example:

NSString *input = @"Hễllö Wõrld! - ếểễệđìíỉĩịòó";
NSString *output = UnicodeUnsign(input);
NSLog(@"%@", output);
// Output: Hello World! - eeeediiiiioo
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • 1
    What is special about Đ and đ that means they need special handling? – JeremyP May 30 '13 at 13:19
  • I have the same problem with "ł" in my transforms. – macbirdie Aug 16 '13 at 09:32
  • @macbirdie: It seems that CFStringTransform removes exactly the "combining marks" that are defined in the "Unicode Data Base", compare http://stackoverflow.com/questions/16187077/making-normalized-version-of-string-in-sqlite-polish-character-l, or http://stackoverflow.com/questions/17418443/normalize-or-canonicalize-string-for-core-data for a similar problem with "Ø". I did not investigate that further, but this thread http://stackoverflow.com/questions/9376621/folding-normalizing-ligatures-e-g-to-ae-using-corefoundation seems to contain useful information. – Martin R Aug 16 '13 at 09:51
  • Yep, looks like it. Thanks for digging out the details. :) – macbirdie Aug 16 '13 at 10:15
0

in case you need this legendary explanation from @Martin R, but written in swift, here it is:

private func unicodeUnsign(_ s: String) -> String {
    var result = s
    result = result.applyingTransform(.stripCombiningMarks, reverse: false) ?? ""
    result = result.replacingOccurrences(of: "đ", with: "d", options: .literal, range: nil)
    result = result.replacingOccurrences(of: "Đ", with: "D", options: .literal, range: nil)
    return result
}
timetraveler90
  • 336
  • 2
  • 16