forked from HaxeFoundation/haxe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringHelper.ml
71 lines (64 loc) · 1.98 KB
/
stringHelper.ml
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
let uppercase s =
let bytes = Bytes.of_string s in
Bytes.iteri
(fun idx char ->
let code = Char.code char in
if 97 <= code && code <= 122 then
Bytes.set bytes idx (Char.chr (code - 32))
)
bytes;
Bytes.to_string bytes
let capitalize s =
if String.length s = 0 then ""
else
let bytes = Bytes.of_string s in
let code = Char.code (Bytes.get bytes 0) in
if 97 <= code && code <= 122 then
Bytes.set bytes 0 (Char.chr (code - 32));
Bytes.to_string bytes
let starts_uppercase_identifier x =
if String.length x = 0 then
raise (Invalid_argument "Identifier name must not be empty")
else
let rec loop p =
match String.unsafe_get x p with
| 'A'..'Z' -> true
| '_' -> p + 1 < String.length x && loop (p + 1)
| _ -> false
in
loop 0
let check_uppercase x =
if String.length x = 0 then
failwith "empty part"
else if not (starts_uppercase_identifier x) then
failwith "Class name must start with an uppercase letter"
let s_escape ?(hex=true) s =
let b = Buffer.create (String.length s) in
for i = 0 to (String.length s) - 1 do
match s.[i] with
| '\n' -> Buffer.add_string b "\\n"
| '\t' -> Buffer.add_string b "\\t"
| '\r' -> Buffer.add_string b "\\r"
| '"' -> Buffer.add_string b "\\\""
| '\\' -> Buffer.add_string b "\\\\"
| c when int_of_char c < 32 && hex -> Buffer.add_string b (Printf.sprintf "\\x%.2X" (int_of_char c))
| c -> Buffer.add_char b c
done;
Buffer.contents b
let escape_res_name name allowed =
ExtString.String.replace_chars (fun chr ->
if (chr >= 'a' && chr <= 'z') || (chr >= 'A' && chr <= 'Z') || (chr >= '0' && chr <= '9') || chr = '_' || chr = '.' then
Char.escaped chr
else if List.mem chr allowed then
Char.escaped chr
else
"-x" ^ (string_of_int (Char.code chr))) name
let remove_extension file =
try String.sub file 0 (String.rindex file '.')
with Not_found -> file
let extension file =
try
let dot_pos = String.rindex file '.' in
String.sub file dot_pos (String.length file - dot_pos)
with Not_found ->
file