1 /*
2  * DSFML - The Simple and Fast Multimedia Library for D
3  *
4  * Copyright (c) 2013 - 2018 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  * DSFML is based on SFML (Copyright Laurent Gomila)
26  */
27 
28 /**
29  * A module containing functions for interacting with strings going to and from
30  * a C/C++ library as well as converting between D's string types. This module
31  * has no dependencies except for std.utf.
32  *
33  * //deprecated: This module is expected to be removed in DSFML 2.5.
34  */
35 //deprecated("This module is expected to be removed in DSFML 2.5.")
36 module nudsfml.system..string;
37 
38 /**
39  * Returns a D string copy of a zero terminated C style string
40  *
41  * Params:
42  * 		str = The C style string to convert
43  *
44  * Returns: The D style string copy.
45  *
46  * //deprecated: This method is expected to be removed in DSFML 2.5.
47  */
48 //deprecated("This method is expected to be removed in DSFML 2.5.")
49 immutable(T)[] toString(T)(in const(T)* str) pure
50 	if (is(T == dchar)||is(T == wchar)||is(T == char))
51 {
52 	if(str is null)
53 		return "";
54 
55 	return str[0..strlen(str)].idup;
56 }
57 
58 /**
59  * Returns the same string in a different utf encoding
60  *
61  * Params:
62  * 		str = The string to convert
63  *
64  * Returns: the C style string pointer.
65  *
66  * //deprecated: Use conversion methonds in std.utf instead.
67  */
68  //deprecated("Use conversion methonds in std.utf instead.")
69 immutable(U)[] stringConvert(T, U)(in T[] str) pure
70 if ((is(T == dchar)||is(T == wchar)||is(T == char)) &&
71 	(is(U == dchar)||is(U == wchar)||is(U == char)))
72 {
73 	import std.utf;
74 
75 	static if(is(U == char))
76 		return toUTF8(str);
77 	else static if(is(U == wchar))
78 		return toUTF16(str);
79 	else
80 		return toUTF32(str);
81 }
82 
83 /**
84  * Get the length of a C style string.
85  *
86  * Params:
87  * 		str = The C style string
88  *
89  * Returns: The C string's length.
90  */
91 private size_t strlen(T)(in const(T)* str) pure nothrow
92 if (is(T == dchar)||is(T == wchar)||is(T == char))
93 {
94 	size_t n = 0;
95 	for (; str[n] != 0; ++n) {}
96 	return n;
97 }
98 
99 unittest
100 {
101 	version(DSFML_Unittest_System)
102 	{
103 		import std.stdio;
104 
105 		writeln("Unit test for string functions");
106 	}
107 }