Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: e45002eb8f
Fetching contributors…

Cannot retrieve contributors at this time

file 157 lines (136 sloc) 4.578 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
//
// NSString+EscapeString.m
// ValueConverter
//
// Created by Uli Kusterer on 27.11.04.
// Copyright 2004 Uli Kusterer.
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//

// -----------------------------------------------------------------------------
// Headers:
// -----------------------------------------------------------------------------

#import "NSString+EscapeString.h"


@implementation NSString (UKEscapeString)

-(NSString*) escapedString
{
    return [self escapedStringUppercase: NO];
}

-(NSString*) uppercaseEscapedString
{
    return [self escapedStringUppercase: YES];
}

-(NSString*) escapedStringUppercase: (BOOL)uc
{
    NSMutableString* outStr = [NSMutableString string];
    int x, count = [self length];
    
    for( x = 0; x < count; x++ )
    {
        unichar ch = [self characterAtIndex: x];
        if( ch >= 0x20 && ch != '\\' )
            [outStr appendString: [NSString stringWithCharacters: &ch length: 1]];
        else
        {
            switch( ch )
            {
                case 0:
                    [outStr appendString: @"\\0"];
                    break;
                
                case '\n':
                    [outStr appendString: @"\\n"];
                    break;
                
                case '\r':
                    [outStr appendString: @"\\r"];
                    break;
                
                case '\t':
                    [outStr appendString: @"\\t"];
                    break;
                
                case '\\':
                    [outStr appendString: @"\\\\"];
                    break;
                
                default:
                    [outStr appendString: [NSString stringWithFormat: (uc ? @"\\x%02X" : @"\\x%02x"), (int)ch]];
            }
        }
    }
    
    return outStr;
}


-(NSString*) unescapedString
{
    NSMutableString* outStr = [NSMutableString string];
    int x, count = [self length];
    
    for( x = 0; x < count; x++ )
    {
        unichar ch = [self characterAtIndex: x];
        if( ch != '\\' )
            [outStr appendString: [NSString stringWithCharacters: &ch length: 1]];
        else
        {
            x++;
            if( x > count )
                break;
            ch = [self characterAtIndex: x];
            if( ch == 'x' ) // Hex.
            {
                x++;
                if( x <= (count -2) )
                {
                    ch = [self characterAtIndex: x++];
                    unichar ch2 = [self characterAtIndex: x];
                    ch = (UKHexToDec(ch) *16) +UKHexToDec(ch2);
                    [outStr appendString: [NSString stringWithCharacters: &ch length: 1]];
                }
            }
            else
            {
                switch( ch )
                {
                    case '0':
                        ch = 0;
                        break;
                    case 'n':
                        ch = '\n';
                        break;
                    case 'r':
                        ch = '\r';
                        break;
                    case 't':
                        ch = '\t';
                        break;
                }
                [outStr appendString: [NSString stringWithCharacters: &ch length: 1]];
            }
        }
    }
    
    return outStr;
}

@end


// Turn a hex-digit character into the corresponding decimal number:
int UKHexToDec( char n )
{
    if( n >= '0' && n <= '9' )
        return n - '0';
    else if( n >= 'A' && n <= 'F' ) // Allow uppercase...
        return 10 +(n -'A');
    else if( n >= 'a' && n <= 'f' ) // ... and lowercase.
        return 10 +(n -'a');
    else
        return 0;
}
Something went wrong with that request. Please try again.