Mercurial > projects > ldc
comparison tango/tango/stdc/stringz.d @ 132:1700239cab2e trunk
[svn r136] MAJOR UNSTABLE UPDATE!!!
Initial commit after moving to Tango instead of Phobos.
Lots of bugfixes...
This build is not suitable for most things.
author | lindquist |
---|---|
date | Fri, 11 Jan 2008 17:57:40 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
131:5825d48b27d1 | 132:1700239cab2e |
---|---|
1 /******************************************************************************* | |
2 | |
3 copyright: Copyright (c) 2006 Keinfarbton. All rights reserved | |
4 | |
5 license: BSD style: $(LICENSE) | |
6 | |
7 version: Initial release: October 2006 | |
8 | |
9 author: Keinfarbton | |
10 | |
11 *******************************************************************************/ | |
12 | |
13 module tango.stdc.stringz; | |
14 | |
15 /********************************* | |
16 * Convert array of chars s[] to a C-style 0 terminated string. | |
17 */ | |
18 | |
19 char* toStringz (char[] s) | |
20 { | |
21 if (s.ptr) | |
22 if (! (s.length && s[$-1] is 0)) | |
23 s = s ~ '\0'; | |
24 return s.ptr; | |
25 } | |
26 | |
27 /********************************* | |
28 * Convert a C-style 0 terminated string to an array of char | |
29 */ | |
30 | |
31 char[] fromUtf8z (char* s) | |
32 { | |
33 return s ? s[0 .. strlenz(s)] : null; | |
34 } | |
35 | |
36 /********************************* | |
37 * Convert array of wchars s[] to a C-style 0 terminated string. | |
38 */ | |
39 | |
40 wchar* toString16z (wchar[] s) | |
41 { | |
42 if (s.ptr) | |
43 if (! (s.length && s[$-1] is 0)) | |
44 s = s ~ "\0"w; | |
45 return s.ptr; | |
46 } | |
47 | |
48 /********************************* | |
49 * Convert a C-style 0 terminated string to an array of wchar | |
50 */ | |
51 | |
52 wchar[] fromUtf16z (wchar* s) | |
53 { | |
54 return s ? s[0 .. strlenz(s)] : null; | |
55 } | |
56 | |
57 /********************************* | |
58 * portable strlen | |
59 */ | |
60 | |
61 size_t strlenz(T) (T* s) | |
62 { | |
63 size_t i; | |
64 | |
65 if (s) | |
66 while (*s++) | |
67 ++i; | |
68 return i; | |
69 } | |
70 | |
71 | |
72 | |
73 debug (UnitTest) | |
74 { | |
75 import tango.stdc.stdio; | |
76 | |
77 unittest | |
78 { | |
79 debug(string) printf("stdc.stringz.unittest\n"); | |
80 | |
81 char* p = toStringz("foo"); | |
82 assert(strlenz(p) == 3); | |
83 char foo[] = "abbzxyzzy"; | |
84 p = toStringz(foo[3..5]); | |
85 assert(strlenz(p) == 2); | |
86 | |
87 char[] test = "\0"; | |
88 p = toStringz(test); | |
89 assert(*p == 0); | |
90 assert(p == test.ptr); | |
91 } | |
92 } | |
93 |