1 /*******************************************************************************
2 
3         copyright:      Copyright (c) 2004 Tango group. All rights reserved
4 
5         license:        BSD style: $(LICENSE)
6 
7         version:        Initial release: July 2006
8 
9 
10         Various char[] utilities
11 
12 *******************************************************************************/
13 
14 module rt.compiler.util..string;
15 
16 private import tango.stdc.string : memcmp;
17 
18 // convert uint to char[], within the given buffer
19 // Returns a valid slice of the populated buffer
20 char[] intToUtf8 (char[] tmp, uint val)
21 in {
22    assert (tmp.length > 9, "atoi buffer should be more than 9 chars wide");
23    }
24 body
25 {
26     char* p = tmp.ptr + tmp.length;
27 
28     do {
29        *--p = cast(char)((val % 10) + '0');
30        } while (val /= 10);
31 
32     return tmp [cast(size_t)(p - tmp.ptr) .. $];
33 }
34 
35 // convert uint to char[], within the given buffer
36 // Returns a valid slice of the populated buffer
37 char[] ulongToUtf8 (char[] tmp, ulong val)
38 in {
39    assert (tmp.length > 19, "atoi buffer should be more than 19 chars wide");
40    }
41 body
42 {
43     char* p = tmp.ptr + tmp.length;
44 
45     do {
46        *--p = cast(char)((val % 10) + '0');
47        } while (val /= 10);
48 
49     return tmp [cast(size_t)(p - tmp.ptr) .. $];
50 }
51 
52 
53 // function to compare two strings
54 int stringCompare (char[] s1, char[] s2)
55 {
56     auto len = s1.length;
57 
58     if (s2.length < len)
59         len = s2.length;
60 
61     int result = memcmp(s1.ptr, s2.ptr, len);
62 
63     if (result == 0)
64         result = (s1.length<s2.length)?-1:((s1.length==s2.length)?0:1);
65 
66     return result;
67 }