Debian Codegraph @

src/core/String.nml

raw
1 /*
2 * Copyright (C)2005-2022 Haxe Foundation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 neko("
24 @string_split = $loader.loadprim('std@string_split',2);
25 ");
26
27 function make(size : int, c : char) : string {
28 neko("
29 var s = $smake(size);
30 var i = 0;
31 while( i < size ) {
32 $sset(s,i,c);
33 i = i + 1;
34 }
35 s
36 ");
37 }
38
39 var create : int -> string = neko("$smake");
40 var length : string -> int = neko("$ssize");
41
42 function get(s : string,n : int) : char {
43 var c = neko("$sget(s,n)");
44 if c == neko("null") then invalid_arg();
45 c
46 }
47
48 function set(s : string, n : int, c : char) : void {
49 if neko("$sset(s,n,c) == null") then invalid_arg();
50 }
51
52 function blit(dst : string, p : int, src : string, p2 : int, l : int ) : void {
53 if neko("try { $sblit(dst,p,src,p2,l); false; } catch e true") then invalid_arg();
54 }
55
56 function sub(s : string, p : int, l : int ) : string {
57 var s = neko("$ssub(s,p,l)");
58 if s == neko("null") then invalid_arg();
59 s
60 }
61
62 function find( s : string, p : int, pat : string ) : int {
63 var pos = neko "$sfind(s,p,pat)";
64 if pos == neko "null" then throw Not_found;
65 pos
66 }
67
68 var list_depency = List.iter
69
70 function split( s : string, sub : string ) : string list {
71 neko("
72 var l = @string_split(s,sub);
73 return @List.@make(l);
74 ");
75 }
76
77 function concat( sep : string, s : string list ) : string {
78 var b = Buffer.create();
79 function rec loop(l) {
80 match l {
81 | [] -> ()
82 | [x] -> Buffer.add b x
83 | x :: l ->
84 Buffer.add b x;
85 Buffer.add b sep;
86 loop l
87 }
88 };
89 loop(s);
90 Buffer.string b
91 }
92
93 function is_printable(c) {
94 c >= '\032' && c <= '\126'
95 }
96
97 function escape_char(c) {
98 match c {
99 | '\n' -> "\\n"
100 | '\t' -> "\\t"
101 | '\r' -> "\\r"
102 | '\\' -> "\\\\"
103 | '"' -> "\\\""
104 | c when !is_printable c -> sprintf "\\%.3d" (ord c)
105 | c -> String.make 1 c
106 }
107 }
108
109 function escape(s) {
110 var b = Buffer.create();
111 function rec loop(i) {
112 if i == String.length s then
113 Buffer.string b
114 else {
115 match String.get s i {
116 | '\n' -> Buffer.add b "\\n"
117 | '\t' -> Buffer.add b "\\t"
118 | '\r' -> Buffer.add b "\\r"
119 | '\\' -> Buffer.add b "\\\\"
120 | '"' -> Buffer.add b "\\\""
121 | c when !is_printable c -> Buffer.add b (sprintf "\\%.3d" (ord c))
122 | c -> Buffer.add_char b c
123 };
124 loop (i+1)
125 }
126 };
127 loop(0);
128 }
129
130 function unescape(s) {
131 var l = length s;
132 var s2 = create l;
133 var i = &0;
134 var p = &0;
135 while *i < l {
136 match neko("$sget")(s,*i) {
137 | '\\' ->
138 i := *i + 1;
139 if *i == l then invalid_arg();
140 var c = neko("$sget")(s,*i);
141 match c {
142 | 'n' ->
143 neko("$sset")(s2,*p,'\n');
144 | 't' ->
145 neko("$sset")(s2,*p,'\t');
146 | 'r' ->
147 neko("$sset")(s2,*p,'\r');
148 | '"' ->
149 neko("$sset")(s2,*p,'"');
150 | '\\' ->
151 neko("$sset")(s2,*p,'\\');
152 | '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' ->
153 if *i + 2 >= l then invalid_arg();
154 var c2 = neko("$sget")(s,*i + 1);
155 var c3 = neko("$sget")(s,*i + 2);
156 i := *i + 2;
157 if c2 < '0' || c2 > '9' || c3 < '0' || c3 > '9' then invalid_arg();
158 var o0 = ord '0';
159 var n = (ord c - o0) * 100 + (ord c2 - o0) * 10 + (ord c3 - o0);
160 if n > 255 then invalid_arg();
161 neko("$sset")(s2,*p,chr n);
162 | _ ->
163 invalid_arg()
164 }
165 | c ->
166 neko("$sset")(s2,*p,c);
167 }
168 p := *p + 1;
169 i := *i + 1;
170 }
171 sub s2 0 (*p)
172 }
173
174
175 function lowercase(s) {
176 var l = length s;
177 var s2 = sub s 0 l;
178 var i = &0;
179 var delta = ord 'a' - ord 'A';
180 while *i < l {
181 var c = neko("$sget")(s,*i);
182 if c >= 'A' && c <= 'Z' then neko("$sset")(s2,*i,chr(ord c + delta));
183 i := *i + 1;
184 }
185 s2
186 }
187
188 function uppercase(s) {
189 var l = length s;
190 var s2 = sub s 0 l;
191 var i = &0;
192 var delta = ord 'A' - ord 'a';
193 while *i < l {
194 var c = neko("$sget")(s,*i);
195 if c >= 'a' && c <= 'z' then neko("$sset")(s2,*i,chr(ord c + delta));
196 i := *i + 1;
197 }
198 s2
199 }
200
201 neko("
202 @serialize = $loader.loadprim('std@serialize',1);
203 @unserialize = $loader.loadprim('std@unserialize',2);
204 ");
205
206 function serialize( x : 'a ) : string {
207 neko "@serialize(x)";
208 }
209
210 function unserialize( x : string ) : 'a {
211 neko "@unserialize(x,$loader)";
212 }