1 /* 2 * DSFML - The Simple and Fast Multimedia Library for D 3 * 4 * Copyright (c) 2013 - 2017 Jeremy DeHaan (dehaan.jeremiah@gmail.com) 5 * 6 * This software is provided 'as-is', without any express or implied warranty. 7 * In no event will the authors be held liable for any damages arising from the 8 * use of this software. 9 * 10 * Permission is granted to anyone to use this software for any purpose, 11 * including commercial applications, and to alter it and redistribute it 12 * freely, subject to the following restrictions: 13 * 14 * 1. The origin of this software must not be misrepresented; you must not claim 15 * that you wrote the original software. If you use this software in a product, 16 * an acknowledgment in the product documentation would be appreciated but is 17 * not required. 18 * 19 * 2. Altered source versions must be plainly marked as such, and must not be 20 * misrepresented as being the original software. 21 * 22 * 3. This notice may not be removed or altered from any source distribution 23 */ 24 25 /** 26 * A module containing functions for interacting with strings going to and from 27 * a C/C++ library as well as converting between D's string types. This module 28 * has no dependencies except for std.utf. 29 */ 30 module dsfml.system..string; 31 32 /** 33 * Returns a D string copy of a zero terminated C style string 34 * 35 * Params: 36 * str = The C style string to convert 37 * 38 * Returns: The D style string copy. 39 */ 40 immutable(T)[] toString(T)(in const(T)* str) pure 41 if (is(T == dchar)||is(T == wchar)||is(T == char)) 42 { 43 return str[0..strlen(str)].idup; 44 } 45 46 /** 47 * Returns the same string in a different utf encoding 48 * 49 * Params: 50 * str = The string to convert 51 * 52 * Returns: the C style string pointer. 53 */ 54 immutable(U)[] stringConvert(T, U)(in T[] str) pure 55 if ((is(T == dchar)||is(T == wchar)||is(T == char)) && 56 (is(U == dchar)||is(U == wchar)||is(U == char))) 57 { 58 import std.utf; 59 60 static if(is(U == char)) 61 return toUTF8(str); 62 else static if(is(U == wchar)) 63 return toUTF16(str); 64 else 65 return toUTF32(str); 66 } 67 68 /** 69 * Get the length of a C style string. 70 * 71 * Params: 72 * str = The C style string 73 * 74 * Returns: The C string's length. 75 */ 76 private size_t strlen(T)(in const(T)* str) pure nothrow 77 if (is(T == dchar)||is(T == wchar)||is(T == char)) 78 { 79 size_t n = 0; 80 for (; str[n] != 0; ++n) {} 81 return n; 82 } 83 84 unittest 85 { 86 version(DSFML_Unittest_System) 87 { 88 import std.stdio; 89 90 writeln("Unit test for string functions"); 91 } 92 93 }