Skip to content

Commit e52ed89

Browse files
committed
add Char module
1 parent 0cc3d8e commit e52ed89

File tree

5 files changed

+235
-0
lines changed

5 files changed

+235
-0
lines changed

src/Core__Char.mjs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as Bytes from "rescript/lib/es6/bytes.js";
4+
import * as PervasivesU from "rescript/lib/es6/pervasivesU.js";
5+
6+
function chr(n) {
7+
if (n < 0 || n > 255) {
8+
return PervasivesU.invalid_arg("Char.chr");
9+
} else {
10+
return n;
11+
}
12+
}
13+
14+
function escaped(param) {
15+
var exit = 0;
16+
if (param >= 40) {
17+
if (param === 92) {
18+
return "\\\\";
19+
}
20+
exit = param >= 127 ? 1 : 2;
21+
} else if (param >= 32) {
22+
if (param >= 39) {
23+
return "\\'";
24+
}
25+
exit = 2;
26+
} else if (param >= 14) {
27+
exit = 1;
28+
} else {
29+
switch (param) {
30+
case 8 :
31+
return "\\b";
32+
case 9 :
33+
return "\\t";
34+
case 10 :
35+
return "\\n";
36+
case 0 :
37+
case 1 :
38+
case 2 :
39+
case 3 :
40+
case 4 :
41+
case 5 :
42+
case 6 :
43+
case 7 :
44+
case 11 :
45+
case 12 :
46+
exit = 1;
47+
break;
48+
case 13 :
49+
return "\\r";
50+
51+
}
52+
}
53+
switch (exit) {
54+
case 1 :
55+
var s = [
56+
0,
57+
0,
58+
0,
59+
0
60+
];
61+
s[0] = /* '\\' */92;
62+
s[1] = 48 + (param / 100 | 0) | 0;
63+
s[2] = 48 + (param / 10 | 0) % 10 | 0;
64+
s[3] = 48 + param % 10 | 0;
65+
return Bytes.to_string(s);
66+
case 2 :
67+
var s$1 = [0];
68+
s$1[0] = param;
69+
return Bytes.to_string(s$1);
70+
71+
}
72+
}
73+
74+
function lowercaseAscii(c) {
75+
if (c >= /* 'A' */65 && c <= /* 'Z' */90) {
76+
return c + 32 | 0;
77+
} else {
78+
return c;
79+
}
80+
}
81+
82+
function uppercaseAscii(c) {
83+
if (c >= /* 'a' */97 && c <= /* 'z' */122) {
84+
return c - 32 | 0;
85+
} else {
86+
return c;
87+
}
88+
}
89+
90+
function compare(c1, c2) {
91+
return c1 - c2 | 0;
92+
}
93+
94+
function equal(c1, c2) {
95+
return (c1 - c2 | 0) === 0;
96+
}
97+
98+
export {
99+
chr ,
100+
escaped ,
101+
lowercaseAscii ,
102+
uppercaseAscii ,
103+
compare ,
104+
equal ,
105+
}
106+
/* No side effect */

src/Core__Char.res

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
OCaml
3+
4+
Xavier Leroy, projet Cristal, INRIA Rocquencourt
5+
6+
Copyright 1996 Institut National de Recherche en Informatique et en Automatique.
7+
8+
All rights reserved. This file is distributed under the terms of
9+
the GNU Lesser General Public License version 2.1, with the
10+
special exception on linking described in the file LICENSE.
11+
*/
12+
13+
14+
type t = char
15+
16+
external code: char => int = "%identity"
17+
external unsafeChr: int => char = "%identity"
18+
19+
let chr = n =>
20+
if n < 0 || n > 255 {
21+
invalid_arg("Char.chr")
22+
} else {
23+
unsafeChr(n)
24+
}
25+
26+
external bytes_create: int => bytes = "?create_bytes"
27+
external bytes_unsafe_set: (bytes, int, char) => unit = "%bytes_unsafe_set"
28+
external unsafe_to_string: bytes => string = "%bytes_to_string"
29+
30+
let escaped = param =>
31+
switch param {
32+
| '\'' => "\\'"
33+
| '\\' => "\\\\"
34+
| '\n' => "\\n"
35+
| '\t' => "\\t"
36+
| '\r' => "\\r"
37+
| '\b' => "\\b"
38+
| ' ' .. '~' as c =>
39+
let s = bytes_create(1)
40+
bytes_unsafe_set(s, 0, c)
41+
unsafe_to_string(s)
42+
| c =>
43+
let n = code(c)
44+
let s = bytes_create(4)
45+
bytes_unsafe_set(s, 0, '\\')
46+
bytes_unsafe_set(s, 1, unsafeChr(48 + n / 100))
47+
bytes_unsafe_set(s, 2, unsafeChr(48 + mod(n / 10, 10)))
48+
bytes_unsafe_set(s, 3, unsafeChr(48 + mod(n, 10)))
49+
unsafe_to_string(s)
50+
}
51+
52+
let lowercaseAscii = c =>
53+
if c >= 'A' && c <= 'Z' {
54+
unsafeChr(code(c) + 32)
55+
} else {
56+
c
57+
}
58+
59+
let uppercaseAscii = c =>
60+
if c >= 'a' && c <= 'z' {
61+
unsafeChr(code(c) - 32)
62+
} else {
63+
c
64+
}
65+
66+
67+
let compare = (c1, c2) => code(c1) - code(c2)
68+
let equal = (c1: t, c2: t) => compare(c1, c2) == 0

src/Core__Char.resi

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
OCaml
3+
4+
Xavier Leroy, projet Cristal, INRIA Rocquencourt
5+
6+
Copyright 1996 Institut National de Recherche en Informatique et en Automatique.
7+
8+
All rights reserved. This file is distributed under the terms of
9+
the GNU Lesser General Public License version 2.1, with the
10+
special exception on linking described in the file LICENSE.
11+
*/
12+
13+
/*** Character operations. */
14+
15+
/** An alias for the type of characters. */
16+
type t = char
17+
18+
/** Return the ASCII code of the argument. */
19+
external code: char => int = "%identity"
20+
21+
/** Return the character with the given ASCII code.
22+
Raise [Invalid_argument "Char.chr"] if the argument is
23+
outside the range 0--255. */
24+
let chr: int => char
25+
26+
/** Return a string representing the given character,
27+
with special characters escaped following the lexical conventions
28+
of OCaml.
29+
All characters outside the ASCII printable range (32..126) are
30+
escaped, as well as backslash, double-quote, and single-quote. */
31+
let escaped: char => string
32+
33+
/** Convert the given character to its equivalent lowercase character,
34+
using the US-ASCII character set.
35+
@since 4.03.0 */
36+
let lowercaseAscii: char => char
37+
38+
/** Convert the given character to its equivalent uppercase character,
39+
using the US-ASCII character set.
40+
@since 4.03.0 */
41+
let uppercaseAscii: char => char
42+
43+
44+
45+
/** The comparison function for characters, with the same specification as
46+
{!Pervasives.compare}. Along with the type [t], this function [compare]
47+
allows the module [Char] to be passed as argument to the functors
48+
{!Set.Make} and {!Map.Make}. */
49+
let compare: (t, t) => int
50+
51+
/** The equal function for chars.
52+
@since 4.03.0 */
53+
let equal: (t, t) => bool
54+
55+
/* The following is for system use only. Do not call directly. */
56+
57+
external unsafeChr: int => char = "%identity"

src/RescriptCore.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ var $$RegExp;
3636

3737
var $$String;
3838

39+
var Char;
40+
3941
var $$Symbol;
4042

4143
var Type;
@@ -116,6 +118,7 @@ export {
116118
$$Promise ,
117119
$$RegExp ,
118120
$$String ,
121+
Char ,
119122
$$Symbol ,
120123
Type ,
121124
$$JSON ,

src/RescriptCore.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module Ordering = Core__Ordering
1717
module Promise = Core__Promise
1818
module RegExp = Core__RegExp
1919
module String = Core__String
20+
module Char = Core__Char
2021
module Symbol = Core__Symbol
2122
module Type = Core__Type
2223
module JSON = Core__JSON

0 commit comments

Comments
 (0)