1 /* 2 3 Boost Software License - Version 1.0 - August 17th, 2003 4 5 Permission is hereby granted, free of charge, to any person or organization 6 obtaining a copy of the software and accompanying documentation covered by 7 this license (the "Software") to use, reproduce, display, distribute, 8 execute, and transmit the Software, and to prepare derivative works of the 9 Software, and to permit third-parties to whom the Software is furnished to 10 do so, all subject to the following: 11 12 The copyright notices in the Software and this entire statement, including 13 the above license grant, this restriction and the following disclaimer, 14 must be included in all copies of the Software, in whole or in part, and 15 all derivative works of the Software, unless such copies or derivative 16 works are solely in the form of machine-executable object code generated by 17 a source language processor. 18 19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 22 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 23 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 DEALINGS IN THE SOFTWARE. 26 27 */ 28 module derelict.sdl.macinit.NSString; 29 30 version(DigitalMars) version(OSX) version = darwin; 31 32 version (darwin): 33 34 version (Tango) 35 { 36 import tango.text.convert.Utf : toString16; 37 import tango.stdc.stringz : toString16z; 38 } 39 40 else 41 import std.utf : toUTF16z; 42 43 import derelict.sdl.macinit.ID; 44 import derelict.sdl.macinit.NSGeometry; 45 import derelict.sdl.macinit.NSObject; 46 import derelict.sdl.macinit.NSZone; 47 import derelict.sdl.macinit.runtime; 48 import derelict.sdl.macinit.selectors; 49 import derelict.sdl.macinit.string; 50 import derelict.util.compat; 51 52 package: 53 54 class NSString : NSObject 55 { 56 this () 57 { 58 id_ = null; 59 } 60 61 this (id id_) 62 { 63 this.id_ = id_; 64 } 65 66 static NSString alloc () 67 { 68 id result = objc_msgSend(cast(id)class_, sel_alloc); 69 return result ? new NSString(result) : null; 70 } 71 72 static Class class_ () 73 { 74 return cast(Class) objc_getClass!(this.stringof); 75 } 76 77 override NSString init () 78 { 79 id result = objc_msgSend(this.id_, sel_init); 80 return result ? this : null; 81 } 82 83 static NSString stringWith (string str) 84 { 85 version (Tango) 86 id result = objc_msgSend(class_NSString, sel_stringWithCharacters_length, toString16z(str.toString16()), str.length); 87 88 else 89 id result = objc_msgSend(class_NSString, sel_stringWithCharacters_length, str.toUTF16z(), str.length); 90 91 return result !is null ? new NSString(result) : null; 92 } 93 94 static NSString opAssign (string str) 95 { 96 return stringWith(str); 97 } 98 99 NSUInteger length () 100 { 101 return cast(NSUInteger) objc_msgSend(this.id_, sel_length); 102 } 103 104 /*const*/ char* UTF8String () 105 { 106 return cast(/*const*/ char*) objc_msgSend(this.id_, sel_UTF8String); 107 } 108 109 void getCharacters (wchar* buffer, NSRange range) 110 { 111 objc_msgSend(this.id_, sel_getCharacters_range, buffer, range); 112 } 113 114 NSString stringWithCharacters (/*const*/ wchar* chars, NSUInteger length) 115 { 116 id result = objc_msgSend(this.id_, sel_stringWithCharacters_length, chars, length); 117 return result ? new NSString(result) : null; 118 } 119 120 NSRange rangeOfString (NSString aString) 121 { 122 return *cast(NSRange*) objc_msgSend(this.id_, sel_rangeOfString, aString ? aString.id_ : null); 123 } 124 125 NSString stringByAppendingString (NSString aString) 126 { 127 id result = objc_msgSend(this.id_, sel_stringByAppendingString, aString ? aString.id_ : null); 128 return result ? new NSString(result) : null; 129 } 130 131 NSString stringByReplacingRange (NSRange aRange, NSString str) 132 { 133 size_t bufferSize; 134 size_t selfLen = this.length; 135 size_t aStringLen = str.length; 136 wchar* buffer; 137 NSRange localRange; 138 NSString result; 139 140 bufferSize = selfLen + aStringLen - aRange.length; 141 buffer = cast(wchar*)NSAllocateMemoryPages(cast(uint)(bufferSize * wchar.sizeof)); 142 143 /* Get first part into buffer */ 144 localRange.location = 0; 145 localRange.length = aRange.location; 146 this.getCharacters(buffer, localRange); 147 148 /* Get middle part into buffer */ 149 localRange.location = 0; 150 localRange.length = aStringLen; 151 str.getCharacters(buffer + aRange.location, localRange); 152 153 /* Get last part into buffer */ 154 localRange.location = aRange.location + aRange.length; 155 localRange.length = selfLen - localRange.location; 156 this.getCharacters(buffer + aRange.location + aStringLen, localRange); 157 158 /* Build output string */ 159 result = NSString.stringWithCharacters(buffer, cast(uint)(bufferSize)); 160 161 NSDeallocateMemoryPages(buffer, cast(uint)(bufferSize)); 162 163 return result; 164 } 165 }