-
Notifications
You must be signed in to change notification settings - Fork 6
/
convert.d
118 lines (111 loc) · 3.56 KB
/
convert.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import iopipe.textpipe;
import iopipe.bufpipe;
import iopipe.buffer;
import iopipe.refc;
import std.io;
import std.range.primitives;
auto stdin()
{
return File(0).refCounted;
}
auto stdout()
{
return File(1).refCounted;
}
void doConvert(UTFType oEnc, Input)(Input input)
{
/* This is the old way it was done. Left here for demonstration purposes
import iopipe.valve;
auto oChain = bufd!(CodeUnit!oEnc) // make a buffered source
.push!(a => a
.encodeText!(oEnc)
.outputPipe(openDev(1))) // push to stdout.
.textOutput; // turn into a standard output range
if(!input.window.empty && input.window.front != 0xfeff)
{
// write a BOM if not present
put(oChain, dchar(0xfeff));
}
foreach(w; input.ensureDecodeable.asInputRange)
put(oChain, w);*/
input.convertText!(CodeUnit!oEnc, true).encodeText!(oEnc).outputPipe(stdout).process();
}
void translate(UTFType iEnc, Input)(Input input, string outputEncoding)
{
import std.conv : to;
auto oEnc = outputEncoding.to!(UTFType);
if(oEnc == iEnc)
{
// straight pass-through
input.outputPipe(stdout).process();
}
else
{
final switch(oEnc)
{
case UTFType.UTF8:
// all other encodings are wider. Need to use converter.
input.assumeText!iEnc.doConvert!(UTFType.UTF8);
break;
case UTFType.UTF16LE:
// check for just changing byte order
static if(iEnc == UTFType.UTF16BE)
{
// just changing byte order. Just do a byte swapper.
input.arrayCastPipe!(ushort).byteSwapper.arrayCastPipe!(ubyte).outputPipe(stdout).process();
}
else
{
// converting widths
input.assumeText!iEnc.doConvert!(UTFType.UTF16LE);
}
break;
case UTFType.UTF16BE:
// check for just changing byte order
static if(iEnc == UTFType.UTF16LE)
{
// just changing byte order. Just do a byte swapper.
input.arrayCastPipe!(ushort).byteSwapper.arrayCastPipe!(ubyte).outputPipe(stdout).process();
}
else
{
// converting widths
input.assumeText!iEnc.doConvert!(UTFType.UTF16BE);
}
break;
case UTFType.UTF32LE:
// check for just changing byte order
static if(iEnc == UTFType.UTF32BE)
{
// just changing byte order. Just do a byte swapper.
input.arrayCastPipe!(uint).byteSwapper.arrayCastPipe!(ubyte).outputPipe(stdout).process();
}
else
{
// converting widths
input.assumeText!iEnc.doConvert!(UTFType.UTF32LE);
}
break;
case UTFType.UTF32BE:
// check for just changing byte order
static if(iEnc == UTFType.UTF32LE)
{
// just changing byte order. Just do a byte swapper.
input.arrayCastPipe!(uint).byteSwapper.arrayCastPipe!(ubyte).outputPipe(stdout).process();
}
else
{
// converting widths
input.assumeText!iEnc.doConvert!(UTFType.UTF32BE);
}
break;
case UTFType.Unknown:
assert(0);
}
}
}
void main(string[] args)
{
// convert all data from input stream to given format
runWithEncoding!(translate)(stdin.bufd, args[1]);
}