Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

root/trunk/tango/stdc/stringz.d

Revision 5600, 3.5 kB (checked in by mwarning, 2 years ago)

fixes #1968 :: reduce/fix 64bit compile warnings for 64bit

  • Property svn:mime-type set to text/x-dsrc
  • Property svn:eol-style set to native
Line 
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 & Kris
10
11 *******************************************************************************/
12
13 module tango.stdc.stringz;
14
15 /*********************************
16  * Convert array of chars to a C-style 0 terminated string.
17  * Providing a tmp will use that instead of the heap, where
18  * appropriate.
19  */
20
21 char* toStringz (char[] s, char[] tmp=null)
22 {
23         static char[] empty = "\0";
24
25         auto len = s.length;
26         if (s.ptr)
27             if (len is 0)
28                 s = empty;
29             else
30                if (s[len-1] != 0)
31                   {
32                   if (tmp.length <= len)
33                       tmp = new char[len+1];
34                   tmp [0..len] = s;
35                   tmp [len] = 0;
36                   s = tmp;
37                   }
38         return s.ptr;
39 }
40
41 /*********************************
42  * Convert a series of char[] to C-style 0 terminated strings, using
43  * tmp as a workspace and dst as a place to put the resulting char*'s.
44  * This is handy for efficiently converting multiple strings at once.
45  *
46  * Returns a populated slice of dst
47  *
48  * Since: 0.99.7
49  */
50
51 char*[] toStringz (char[] tmp, char*[] dst, char[][] strings...)
52 {
53         assert (dst.length >= strings.length);
54
55         auto len = strings.length;
56         foreach (s; strings)
57                  len += s.length;
58         if (tmp.length < len)
59             tmp.length = len;
60
61         foreach (i, s; strings)
62                 {
63                 dst[i] = toStringz (s, tmp);
64                 tmp = tmp [s.length + 1 .. len];
65                 }
66         return dst [0 .. strings.length];
67 }
68
69 /*********************************
70  * Convert a C-style 0 terminated string to an array of char
71  */
72
73 char[] fromStringz (char* s)
74 {
75         return s ? s[0 .. strlenz(s)] : null;
76 }
77
78 /*********************************
79  * Convert array of wchars s[] to a C-style 0 terminated string.
80  */
81
82 wchar* toString16z (wchar[] s)
83 {
84         if (s.ptr)
85             if (! (s.length && s[$-1] is 0))
86                    s = s ~ "\0"w;
87         return s.ptr;
88 }
89
90 /*********************************
91  * Convert a C-style 0 terminated string to an array of wchar
92  */
93
94 wchar[] fromString16z (wchar* s)
95 {
96         return s ? s[0 .. strlenz(s)] : null;
97 }
98
99 /*********************************
100  * Convert array of dchars s[] to a C-style 0 terminated string.
101  */
102
103 dchar* toString32z (dchar[] s)
104 {
105         if (s.ptr)
106             if (! (s.length && s[$-1] is 0))
107                    s = s ~ "\0"d;
108         return s.ptr;
109 }
110
111 /*********************************
112  * Convert a C-style 0 terminated string to an array of dchar
113  */
114
115 dchar[] fromString32z (dchar* s)
116 {
117         return s ? s[0 .. strlenz(s)] : null;
118 }
119
120 /*********************************
121  * portable strlen
122  */
123
124 size_t strlenz(T) (T* s)
125 {
126         size_t i;
127
128         if (s)
129             while (*s++)
130                    ++i;
131         return i;
132 }
133
134
135
136 debug (UnitTest)
137 {
138         import tango.stdc.stdio;
139
140         unittest
141         {
142         debug(string) printf("stdc.stringz.unittest\n");
143
144         char* p = toStringz("foo");
145         assert(strlenz(p) == 3);
146         char[] foo = "abbzxyzzy";
147         p = toStringz(foo[3..5]);
148         assert(strlenz(p) == 2);
149
150         char[] test = "\0";
151         p = toStringz(test);
152         assert(*p == 0);
153         assert(p == test.ptr);
154         }
155 }
Note: See TracBrowser for help on using the browser.