Landin library reference discuss source

platform/windows/wide.ldn

1--  Host strings and the system's 16-bit units.  A Windows host string is
2--  WTF-8 (D2268): the units the system gave, each scalar value UTF-8
3--  encodes as UTF-8 does, a surrogate pair as its one scalar, and a lone
4--  surrogate as the three bytes UTF-8 would give its value, so every
5--  sequence of units the system names a file or an argument by survives
6--  the trip to bytes and back, and one that is well-formed UTF-16 is
7--  UTF-8 text.  A call that takes a terminated wide string has the units
8--  copied into a buffer of the caller's with a NUL after them, as
9--  platform/posix terminates a path.
10
11import core/text
12
13fixed if compiler.os == windows then
14    --- A host string this module cannot give the system: one holding a NUL
15    --- or bytes WTF-8 does not encode, `ERROR_INVALID_NAME` to the system.
16    public bad_name: atom
17
18    --- A host string whose units and NUL do not fit the buffer given,
19    --- `ERROR_FILENAME_EXCED_RANGE` to the system.
20    public name_too_long: atom
21
22    --- Why a host string could not be given to the system.
23    public name_failure: type = bad_name | name_too_long
24
25    --- The Win32 error code that says the same as `problem`.
26    public code_of: (problem: name_failure) -> (code: u32) =
27        code = match problem
28            bad_name: error_invalid_name
29            name_too_long: error_filename_exceeds_range
30        end match
31    end code_of
32
33    --- Room for a path and its NUL, in units: Windows' long-path limit is
34    --- 32,767, but a path beyond `MAX_PATH`, 260, needs the `\\?\` form
35    --- and a manifest the image does not carry, so this room keeps a frame
36    --- small and still takes every path the system gives an ordinary
37    --- program.
38    public path_room: type = [1024]u16
39
40    leading: (unit: u16) -> (yes: bool) =
41        yes = unit >= 0xD800 and unit <= 0xDBFF
42    end leading
43
44    trailing: (unit: u16) -> (yes: bool) =
45        yes = unit >= 0xDC00 and unit <= 0xDFFF
46    end trailing
47
48    --- How many bytes `to_host` writes for `units`.
49    public host_length: (units: []u16) -> (length: usize) =
50        length = 0
51        mut at: usize = 0
52        while at < lenof units do
53            unit: u16 = units[at]
54            if unit < 0x80 then
55                length += 1
56            elsif unit < 0x800 then
57                length += 2
58            elsif leading(unit) and at + 1 < lenof units
59              and trailing(units[at + 1]) then
60                length += 4
61                inc at
62            else
63                length += 3
64            end if
65            inc at
66        end while
67    end host_length
68
69    --- Write `units` as WTF-8 into `into`, which has room for `host_length`
70    --- bytes of them, and return how many it wrote.
71    public to_host: (units: []u16, into: []mut u8) -> (count: usize) =
72        count = 0
73        mut at: usize = 0
74        while at < lenof units do
75            mut value: u32 = u32(units[at])
76            if leading(units[at]) and at + 1 < lenof units
77              and trailing(units[at + 1]) then
78                value = 0x10000 + ((value - 0xD800) << 10)
79                        + (u32(units[at + 1]) - 0xDC00)
80                inc at
81            end if
82            inc at
83            if value < 0x80 then
84                into[count] = u8(value)
85                count += 1
86            elsif value < 0x800 then
87                into[count] = u8(0xC0 | (value >> 6))
88                into[count + 1] = u8(0x80 | (value & 0x3F))
89                count += 2
90            elsif value < 0x10000 then
91                into[count] = u8(0xE0 | (value >> 12))
92                into[count + 1] = u8(0x80 | ((value >> 6) & 0x3F))
93                into[count + 2] = u8(0x80 | (value & 0x3F))
94                count += 3
95            else
96                into[count] = u8(0xF0 | (value >> 18))
97                into[count + 1] = u8(0x80 | ((value >> 12) & 0x3F))
98                into[count + 2] = u8(0x80 | ((value >> 6) & 0x3F))
99                into[count + 3] = u8(0x80 | (value & 0x3F))
100                count += 4
101            end if
102        end while
103    end to_host
104
105    continuation: (bytes: []u8, at: usize) -> (value: u32) ! bad_name =
106        fail bad_name when at >= lenof bytes
107        fail bad_name when (bytes[at] & 0xC0) <> 0x80
108        value = u32(bytes[at] & 0x3F)
109    end continuation
110
111    --- Write the units of the WTF-8 `bytes` into `into` and return how many:
112    --- `bad_name` for bytes WTF-8 does not encode or a NUL, `name_too_long`
113    --- when they do not fit.
114    public to_units: (bytes: []u8, into: []mut u16) -> (count: usize)
115                     ! name_failure =
116        count = 0
117        mut at: usize = 0
118        while at < lenof bytes do
119            first: u8 = bytes[at]
120            mut value: u32 = 0
121            if first < 0x80 then
122                value = u32(first)
123                at += 1
124            elsif (first & 0xE0) == 0xC0 then
125                value = (u32(first & 0x1F) << 6) | try continuation(bytes, at + 1)
126                fail bad_name when value < 0x80
127                at += 2
128            elsif (first & 0xF0) == 0xE0 then
129                value = (u32(first & 0x0F) << 12)
130                        | (try continuation(bytes, at + 1) << 6)
131                        | try continuation(bytes, at + 2)
132                fail bad_name when value < 0x800
133                at += 3
134            elsif (first & 0xF8) == 0xF0 then
135                value = (u32(first & 0x07) << 18)
136                        | (try continuation(bytes, at + 1) << 12)
137                        | (try continuation(bytes, at + 2) << 6)
138                        | try continuation(bytes, at + 3)
139                fail bad_name when value < 0x10000 or value > 0x10FFFF
140                at += 4
141            else
142                fail bad_name
143            end if
144            fail bad_name when value == 0
145            if value >= 0x10000 then
146                fail name_too_long when count + 2 > lenof into
147                rest: u32 = value - 0x10000
148                into[count] = u16(0xD800 + (rest >> 10))
149                into[count + 1] = u16(0xDC00 + (rest & 0x3FF))
150                count += 2
151            else
152                fail name_too_long when count + 1 > lenof into
153                into[count] = u16(value)
154                count += 1
155            end if
156        end while
157    end to_units
158
159    --- Copy `name` into `room` as units with a NUL after them, for a call
160    --- that takes a terminated wide string, and return the first unit's
161    --- address: `bad_name` for a name holding a NUL or bytes WTF-8 does not
162    --- encode, `name_too_long` when it and its NUL do not fit.
163    public terminate: (name: text.os_string, room: []mut u16)
164                      -> (terminated: ptr u16 from room) ! name_failure =
165        fail name_too_long when lenof room == 0
166        count: usize = try to_units(text.os_string_bytes(name),
167                                    room[0..<lenof room - 1])
168        room[count] = 0
169        terminated = addr room[0]
170    end terminate
171
172end if