how to test if unichar is upper or lower case?

Discussion in 'Mac Programming' started by teek, Feb 26, 2010.

  1. teek macrumors member

    teek

    Joined:
    Feb 12, 2008
    Location:
    Norway
    #1
    unichar c = 'c';

    if(c is lowercase) make it uppercase;

    or

    make c uppercase regardless of current case.

    How do I do that ?
     
  2. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    You should never try to manipulate individual Unicode code points. You don't say what the type "unichar" is, but it may not be a complete code point, and it is most likely not a complete character. Then there are characters that come in three modes: lower case, upper case, and title case - The Title Case Would Be Used In A Sentence Like This, AND UPPERCASE LIKE THIS.

    Check out CFStringCapitalize, CFStringUppercase and CFStringLowercase or the NSString equivalents.
     
  3. teek thread starter macrumors member

    teek

    Joined:
    Feb 12, 2008
    Location:
    Norway
    #3
    Ok

    I will look at those but to clear things up:

    typedef unsigned short unichar;

    It is returned from NSString instance method:

    -(unichar)characterAtIndex:(NSUInteger)index
     
  4. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #4
    Well, NSStrings seem to use UTF-16, which means every Unicode code point (there are more than a million of them) is stored as one or two 16 bit values. So most likely you get a Unicode code point, but sometimes you only get half.

    But open "Character Viewer" and look at codes 01f1, 01f2 and 01fe (DZ, Dz and dz). These three are single Unicode characters! And when a sentence starts with one of these, and the first letter in a sentence should be capitalised, you need the Dz and not the DZ. Edit my post, and try clicking between d and z: You can't.

    The correct thing is to use the functions that are provided, because lots of things in Unicode are just not simple. Even converting a character to uppercase is not simple.
     
  5. plinden macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #5
    Check the NSCharacterSet documentation to find out how to check whether a character is upper or lower, or the three NSString "changing case" methods to change regardless.

    NSCharacterSet
    NSString
     
  6. teek thread starter macrumors member

    teek

    Joined:
    Feb 12, 2008
    Location:
    Norway
    #6
    Okay I get your point, this is great feedback.
    But what if I limit the conversion to apply only if the characters are within the character range a-z and A-Z. Then I guess it's pretty safe to convert between upper and lower case, or are there other issues that I don't know about?
     
  7. plinden macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #7
    No need to restrict anything if you don't bother rolling your own solution. If you're changing the whole string, just use:

    NSString *s = @"some text with UPPER and lower case letters";
    NSString *upper = [s upperCaseString];

    Or:
    BOOL isLowerCase = [[NSCharacterSet lowercaseLetterCharacterSet] characterIsMember:[s characterAtIndex:index]];

    If you want to change only the character at index, you'll need to use NSMutableString and replaceCharactersInRange:withString:

    Let the framework do the work for you.
     
  8. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #8
    What if your code may be used by Владимир Юшенко? Other languages do have cases as well, and the NSString functions, if I understand correctly, will make the proper case changes in those situations. It is probably best to rely on NSString to handle these things for you and not try to do it manually.
     
  9. teek thread starter macrumors member

    teek

    Joined:
    Feb 12, 2008
    Location:
    Norway
    #9
    Ok I won't dig any deeper into this and I'll just go with NSString/NSMutableString NSCharacterSet and the lot. Thank you so much for your replies :)
     

Share This Page