1 /* 2 Copyright (C) 2007-2010 Christopher E. Miller 3 4 This software is provided 'as-is', without any express or implied 5 warranty. In no event will the authors be held liable for any damages 6 arising from the use of this software. 7 8 Permission is granted to anyone to use this software for any purpose, 9 including commercial applications, and to alter it and redistribute it 10 freely, subject to the following restrictions: 11 12 1. The origin of this software must not be misrepresented; you must not 13 claim that you wrote the original software. If you use this software 14 in a product, an acknowledgment in the product documentation would be 15 appreciated but is not required. 16 2. Altered source versions must be plainly marked as such, and must not be 17 misrepresented as being the original software. 18 3. This notice may not be removed or altered from any source distribution. 19 */ 20 21 22 module dfl.internal.dlib; 23 24 25 alias typeof(""c[]) Dstring; 26 alias typeof(""c.ptr) Dstringz; 27 alias typeof(" "c[0]) Dchar; 28 alias typeof(""w[]) Dwstring; 29 alias typeof(""w.ptr) Dwstringz; 30 alias typeof(" "w[0]) Dwchar; 31 alias typeof(""d[]) Ddstring; 32 alias typeof(""d.ptr) Ddstringz; 33 alias typeof(" "d[0]) Ddchar; 34 35 36 version(D_Version2) { 37 version = DFL_D2; 38 version = DFL_D2_AND_ABOVE; 39 } 40 else version(D_Version3) { 41 version = DFL_D3; 42 version = DFL_D3_AND_ABOVE; 43 version = DFL_D2_AND_ABOVE; 44 } 45 else version(D_Version4) { 46 version = DFL_D4; 47 version = DFL_D4_AND_ABOVE; 48 version = DFL_D3_AND_ABOVE; 49 version = DFL_D2_AND_ABOVE; 50 } 51 else { 52 version = DFL_D1; 53 } 54 //version = DFL_D1_AND_ABOVE; 55 56 57 version(DFL_D1) { 58 public import dfl.internal.d1; 59 } 60 else { 61 public import dfl.internal.d2; 62 } 63 64 65 version(DFL_D1) { 66 version(DFL_USE_CORE_MEMORY) { 67 } 68 else { 69 version = DFL_NO_USE_CORE_MEMORY; 70 version = _DFL_NO_USE_CORE_EXCEPTION_OUTOFMEMORY_EXCEPTION; 71 } 72 73 version(DFL_CONV_TO_TEMPLATE) { 74 } 75 else { 76 version = DFL_NO_CONV_TO_TEMPLATE; 77 } 78 } 79 80 81 version(DFL_D2_AND_ABOVE) { 82 version(DFL_beforeDMD2020) { 83 version = DFL_NO_USE_CORE_MEMORY; 84 version = _DFL_NO_USE_CORE_EXCEPTION_OUTOFMEMORY_EXCEPTION; 85 version = _DFL_NO_USE_CORE_EXCEPTION_OUTOFMEMORY_ERROR; 86 87 version = DFL_beforeDMD2021; 88 version = DFL_beforeDMD2029; 89 } 90 91 version(DFL_beforeDMD2021) { 92 version = _DFL_NO_USE_CORE_EXCEPTION_OUTOFMEMORY_ERROR; 93 94 version = DFL_beforeDMD2029; 95 } 96 97 version(DFL_beforeDMD2029) { 98 version(DFL_CONV_TO_TEMPLATE) { 99 } 100 else { 101 version = DFL_NO_CONV_TO_TEMPLATE; 102 } 103 } 104 } 105 106 107 version(DFL_NO_USE_CORE_MEMORY) { 108 version = _DFL_NO_USE_CORE_EXCEPTION_OUTOFMEMORY_EXCEPTION; 109 } 110 111 112 public import std.traits; 113 114 115 alias ReturnType!(Object.opEquals) Dequ; // Since D2 changes mid-stream. 116 117 118 Dstring getObjectString(Object o) { 119 return o.toString(); 120 } 121 122 123 version(DFL_NO_USE_CORE_MEMORY) { 124 private import std.gc; // If you get "module gc cannot read file 'core\memory.d'" then use -version=DFL_NO_USE_CORE_MEMORY <http://wiki.dprogramming.com/Dfl/CompileVersions> 125 126 void gcPin(void* p) { } 127 void gcUnpin(void* p) { } 128 129 deprecated alias std.gc.genCollect gcGenCollect; 130 131 alias std.gc.fullCollect gcFullCollect; 132 } 133 else { 134 private import core.memory; // If you get "module gc cannot read file 'std\gc.d'" then use -version=DFL_USE_CORE_MEMORY <http://wiki.dprogramming.com/Dfl/CompileVersions> 135 136 void gcPin(void* p) { } 137 void gcUnpin(void* p) { } 138 139 deprecated void gcGenCollect() { 140 core.memory.GC.collect(); 141 } 142 143 void gcFullCollect() nothrow { 144 try 145 { 146 core.memory.GC.collect(); 147 } catch (Throwable e) { 148 } 149 } 150 } 151 152 153 private import std.string; 154 155 alias std..string.icmp stringICmp; 156 157 version(DFL_NO_CONV_TO_TEMPLATE) { 158 alias std..string.toString stringFromStringz; 159 } 160 else { 161 version(DFL_DMD2029) { 162 Dstring stringFromStringz(Dstringz sz) { 163 return std.conv.to!(Dstring, Dstringz)(sz); // D 2.029 164 } 165 } 166 else { 167 Dstring stringFromStringz(Dstringz sz) { 168 return std.conv.to!(Dstring)(sz); 169 } 170 } 171 172 version(DFL_D2_AND_ABOVE) { 173 Dstring stringFromStringz(char* sz) { 174 return stringFromStringz(cast(Dstringz)sz); 175 } 176 } 177 } 178 179 alias std..string.split stringSplit; 180 181 version(DFL_NO_CONV_TO_TEMPLATE) { 182 alias std..string.toString intToString; 183 } 184 else { 185 Dstring intToString(int i) { 186 return to!(Dstring)(i); // D 2.029 187 } 188 } 189 190 alias std.algorithm.find charFindInString; 191 192 alias std..string.toStringz stringToStringz; 193 194 Dstring uintToHexString(uint num) { 195 return std..string.format("%X", num); 196 } 197 198 alias std..string.splitLines stringSplitLines; 199 200 201 private import std.path; 202 203 alias std.path.dirName pathGetDirName; 204 205 version(D_Version2) 206 alias std.ascii.newline nativeLineSeparatorString; 207 else { 208 alias std.path.linesep nativeLineSeparatorString; 209 } 210 211 alias std.path.buildPath pathJoin; 212 213 alias std.path.pathSeparator nativePathSeparatorString; 214 215 216 version(_DFL_NO_USE_CORE_EXCEPTION_OUTOFMEMORY_EXCEPTION) { 217 private import std.outofmemory; 218 219 alias std.outofmemory.OutOfMemoryException OomException; 220 } 221 else { 222 private import core.exception; 223 224 version(_DFL_NO_USE_CORE_EXCEPTION_OUTOFMEMORY_ERROR) { 225 class OomException: core.exception.OutOfMemoryException { 226 this() { 227 super(null, 0); 228 } 229 } 230 } 231 else { 232 class OomException: core.exception.OutOfMemoryError { 233 this() { 234 super(null, 0); 235 } 236 } 237 } 238 } 239 240 241 private import std.utf; 242 243 alias std.utf.decode utf8stringGetUtf32char; 244 245 alias std.utf.toUTF8 utf16stringtoUtf8string; 246 247 alias std.utf.toUTF16 utf8stringtoUtf16string; 248 249 alias std.utf.toUTFz!(typeof(Dwstring.init.ptr)) utf8stringToUtf16stringz; 250 251 alias std.utf.toUTF8 utf32stringtoUtf8string; 252 253 alias std.utf.toUTF32 utf8stringtoUtf32string; 254 255 256 private import std.uni; 257 258 alias std.uni.toLower utf32charToLower; 259 260 261 private import std.conv; 262 263 version(DFL_NO_CONV_TO_TEMPLATE) { 264 alias std.conv.toInt stringToInt; 265 } 266 else { 267 version(DFL_DMD2029) { 268 alias std.conv.to!(int, Dstring) stringToInt; // D 2.029 269 } 270 else { 271 int stringToInt(Dstring s) { 272 return std.conv.to!(int)(s); 273 } 274 } 275 } 276 277 278 private import std.ascii; 279 280 alias std.ascii.isHexDigit charIsHexDigit; 281 282 283 private import std.stream; 284 285 alias std.stream.Stream DStream; 286 287 alias std.stream.OutputStream DOutputStream; 288 289 alias std.stream.StreamException DStreamException; 290 291 292 alias Object DObject; 293 version(DFL_D2_AND_ABOVE) { 294 version(DFL_CanThrowObject) { 295 alias Object DThrowable; 296 } 297 else { 298 alias Throwable DThrowable; 299 } 300 } 301 else { 302 alias Object DThrowable; 303 } 304 305 306 char* unsafeToStringz(Dstring s) { 307 // This is intentionally unsafe, hence the name. 308 if(!s.ptr[s.length]) 309 //return s.ptr; 310 { 311 return cast(char*)s.ptr; // Needed in D2. 312 } 313 //return stringToStringz(s); 314 return cast(char*)stringToStringz(s); // Needed in D2. 315 } 316