1 |
import tango.convert.Atoi, |
---|
2 |
tango.convert.Double, |
---|
3 |
tango.convert.DGDouble, |
---|
4 |
tango.convert.Format, |
---|
5 |
tango.convert.Integer, |
---|
6 |
tango.convert.Rfc1123, |
---|
7 |
tango.convert.Sprint, |
---|
8 |
tango.convert.Unicode, |
---|
9 |
tango.convert.UnicodeBom, |
---|
10 |
tango.convert.Type, |
---|
11 |
tango.io.Stdout; |
---|
12 |
|
---|
13 |
|
---|
14 |
|
---|
15 |
|
---|
16 |
void main () |
---|
17 |
{ |
---|
18 |
|
---|
19 |
char [] i = "+2100"; |
---|
20 |
char [] r = "2006.21"; |
---|
21 |
char [] d = "2006.314159"; |
---|
22 |
|
---|
23 |
|
---|
24 |
/* String to Number conversions */ |
---|
25 |
|
---|
26 |
|
---|
27 |
int integer = Atoi.parse(i); |
---|
28 |
|
---|
29 |
int alternate = Integer.parse(i); |
---|
30 |
|
---|
31 |
double double_ = Double.parse(r); |
---|
32 |
|
---|
33 |
real real_ = DGDouble.parse(d); // for extended accuracy |
---|
34 |
|
---|
35 |
Stdout("Integer [ %d:%d ], DGDouble [ %r ], Double [ %d ]",integer,alternate,real_ , double_).newline; |
---|
36 |
|
---|
37 |
|
---|
38 |
/* Number to String conversions */ |
---|
39 |
|
---|
40 |
|
---|
41 |
char [64] intBuffer; |
---|
42 |
char [64] floatBuffer; |
---|
43 |
char [64] realBuffer; |
---|
44 |
|
---|
45 |
char [] intString = Integer.format(intBuffer,32 ); |
---|
46 |
|
---|
47 |
char [] floatString = Double.format(floatBuffer,45 ); |
---|
48 |
|
---|
49 |
char [] realString = DGDouble.format(realBuffer,32.65798,3 ); |
---|
50 |
|
---|
51 |
Stdout("Integer [ %s ], Double [ %s ], DGDouble [ %s ] ",intString,floatString,realString ).newline; |
---|
52 |
|
---|
53 |
|
---|
54 |
|
---|
55 |
/* HTTP date conversions , accepts strings in RFC1123 format*/ |
---|
56 |
|
---|
57 |
char [] date = "Sun, 06 Nov 1994 08:49:37 GMT"; |
---|
58 |
|
---|
59 |
ulong secondsSinceEpoch = Rfc1123.parse(date); |
---|
60 |
|
---|
61 |
char [100] dateBuffer; |
---|
62 |
|
---|
63 |
char [] dateString = Rfc1123.format(dateBuffer,secondsSinceEpoch); |
---|
64 |
|
---|
65 |
Stdout("secondsSinceEpoch [ %d] , dateString [ %s ]",secondsSinceEpoch,dateString ).newline; |
---|
66 |
|
---|
67 |
|
---|
68 |
/* String formatting , replaces vsprintf() style functions*/ |
---|
69 |
|
---|
70 |
// struct based sprint with no precision specifiers, all stack based |
---|
71 |
|
---|
72 |
char [100] stackSprintString; |
---|
73 |
|
---|
74 |
SprintStruct stackSprint; |
---|
75 |
|
---|
76 |
stackSprint.ctor(stackSprintString); |
---|
77 |
|
---|
78 |
char [] stringWithNoPrecision = stackSprint("%s %s %s - %d ","All your base","are","belong to us",2100 ); |
---|
79 |
|
---|
80 |
Stdout(stringWithNoPrecision).newline; |
---|
81 |
|
---|
82 |
// class based sprint with precision specifiers, we use Double.format |
---|
83 |
|
---|
84 |
Sprint sprint = new Sprint(100, &Double.format); |
---|
85 |
|
---|
86 |
char [] stringWithPrecision = sprint ( "%s is %d times %2.3f","Julio",32, 5.68732 ); |
---|
87 |
|
---|
88 |
Stdout(stringWithPrecision).newline; |
---|
89 |
|
---|
90 |
|
---|
91 |
/* converting to / from UTF8, UTF16, UTF32, both Big Endian and Little Endian */ |
---|
92 |
|
---|
93 |
char [] str = "Marry had a little endian"; |
---|
94 |
|
---|
95 |
wchar [] wstr = Unicode.toUtf16(str); |
---|
96 |
|
---|
97 |
dchar [] dstr = Unicode.toUtf32(str); |
---|
98 |
|
---|
99 |
char [] newStr = Unicode.toUtf8(wstr); // and back again |
---|
100 |
|
---|
101 |
newStr = Unicode.toUtf8(dstr); //and back again |
---|
102 |
|
---|
103 |
Stdout(newStr).newline; |
---|
104 |
|
---|
105 |
// or the alternate way |
---|
106 |
|
---|
107 |
Unicode.Into!(char) into; // char is what converting into |
---|
108 |
|
---|
109 |
char [] catchAllConverter = cast(char[])into.convert(cast(void[])wstr /* what you want to convert*/,Type.Utf16 /* the type the your converting _from_ */); |
---|
110 |
Stdout(catchAllConverter).newline; |
---|
111 |
|
---|
112 |
|
---|
113 |
/* Converting content to unicode for writing files. See alsio tango.io.UnicodeFile .*/ |
---|
114 |
|
---|
115 |
char [] bubbleBoy = "What you've never seen anyone in a bubble before ?"; |
---|
116 |
|
---|
117 |
// since were just encoding and not reading in ( if we were reading in the encoding would be figured out by the BOM ) , have to sepcify big or little endian |
---|
118 |
|
---|
119 |
UnicodeBomT!(char) bom = new UnicodeBomT!(char)(Unicode.UTF_16BE); |
---|
120 |
|
---|
121 |
void [] encoded = bom.encode(bubbleBoy ); |
---|
122 |
|
---|
123 |
Stdout(cast(char[])bubbleBoy ); |
---|
124 |
|
---|
125 |
|
---|
126 |
} |
---|