Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Char module #229

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions src/Core__Char.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Bytes from "rescript/lib/es6/bytes.js";
import * as PervasivesU from "rescript/lib/es6/pervasivesU.js";

function chr(n) {
if (n < 0 || n > 255) {
return PervasivesU.invalid_arg("Char.chr");
} else {
return n;
}
}

function escaped(param) {
var exit = 0;
if (param >= 40) {
if (param === 92) {
return "\\\\";
}
exit = param >= 127 ? 1 : 2;
} else if (param >= 32) {
if (param >= 39) {
return "\\'";
}
exit = 2;
} else if (param >= 14) {
exit = 1;
} else {
switch (param) {
case 8 :
return "\\b";
case 9 :
return "\\t";
case 10 :
return "\\n";
case 0 :
case 1 :
case 2 :
case 3 :
case 4 :
case 5 :
case 6 :
case 7 :
case 11 :
case 12 :
exit = 1;
break;
case 13 :
return "\\r";

}
}
switch (exit) {
case 1 :
var s = [
0,
0,
0,
0
];
s[0] = /* '\\' */92;
s[1] = 48 + (param / 100 | 0) | 0;
s[2] = 48 + (param / 10 | 0) % 10 | 0;
s[3] = 48 + param % 10 | 0;
return Bytes.to_string(s);
case 2 :
var s$1 = [0];
s$1[0] = param;
return Bytes.to_string(s$1);

}
}

function lowercaseAscii(c) {
if (c >= /* 'A' */65 && c <= /* 'Z' */90) {
return c + 32 | 0;
} else {
return c;
}
}

function uppercaseAscii(c) {
if (c >= /* 'a' */97 && c <= /* 'z' */122) {
return c - 32 | 0;
} else {
return c;
}
}

function compare(c1, c2) {
return c1 - c2 | 0;
}

function equal(c1, c2) {
return (c1 - c2 | 0) === 0;
}

export {
chr ,
escaped ,
lowercaseAscii ,
uppercaseAscii ,
compare ,
equal ,
}
/* No side effect */
68 changes: 68 additions & 0 deletions src/Core__Char.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
OCaml

Xavier Leroy, projet Cristal, INRIA Rocquencourt

Copyright 1996 Institut National de Recherche en Informatique et en Automatique.

All rights reserved. This file is distributed under the terms of
the GNU Lesser General Public License version 2.1, with the
special exception on linking described in the file LICENSE.
*/


type t = char

external code: char => int = "%identity"
external unsafeChr: int => char = "%identity"

let chr = n =>
if n < 0 || n > 255 {
invalid_arg("Char.chr")
} else {
unsafeChr(n)
}

external bytes_create: int => bytes = "?create_bytes"
external bytes_unsafe_set: (bytes, int, char) => unit = "%bytes_unsafe_set"
external unsafe_to_string: bytes => string = "%bytes_to_string"

let escaped = param =>
switch param {
| '\'' => "\\'"
| '\\' => "\\\\"
| '\n' => "\\n"
| '\t' => "\\t"
| '\r' => "\\r"
| '\b' => "\\b"
| ' ' .. '~' as c =>
let s = bytes_create(1)
bytes_unsafe_set(s, 0, c)
unsafe_to_string(s)
| c =>
let n = code(c)
let s = bytes_create(4)
bytes_unsafe_set(s, 0, '\\')
bytes_unsafe_set(s, 1, unsafeChr(48 + n / 100))
bytes_unsafe_set(s, 2, unsafeChr(48 + mod(n / 10, 10)))
bytes_unsafe_set(s, 3, unsafeChr(48 + mod(n, 10)))
unsafe_to_string(s)
}

let lowercaseAscii = c =>
if c >= 'A' && c <= 'Z' {
unsafeChr(code(c) + 32)
} else {
c
}

let uppercaseAscii = c =>
if c >= 'a' && c <= 'z' {
unsafeChr(code(c) - 32)
} else {
c
}


let compare = (c1, c2) => code(c1) - code(c2)
let equal = (c1: t, c2: t) => compare(c1, c2) == 0
57 changes: 57 additions & 0 deletions src/Core__Char.resi
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
OCaml

Xavier Leroy, projet Cristal, INRIA Rocquencourt

Copyright 1996 Institut National de Recherche en Informatique et en Automatique.

All rights reserved. This file is distributed under the terms of
the GNU Lesser General Public License version 2.1, with the
special exception on linking described in the file LICENSE.
*/

/*** Character operations. */

/** An alias for the type of characters. */
type t = char

/** Return the ASCII code of the argument. */
external code: char => int = "%identity"

/** Return the character with the given ASCII code.
Raise [Invalid_argument "Char.chr"] if the argument is
outside the range 0--255. */
let chr: int => char

/** Return a string representing the given character,
with special characters escaped following the lexical conventions
of OCaml.
All characters outside the ASCII printable range (32..126) are
escaped, as well as backslash, double-quote, and single-quote. */
let escaped: char => string

/** Convert the given character to its equivalent lowercase character,
using the US-ASCII character set.
@since 4.03.0 */
let lowercaseAscii: char => char

/** Convert the given character to its equivalent uppercase character,
using the US-ASCII character set.
@since 4.03.0 */
let uppercaseAscii: char => char



/** The comparison function for characters, with the same specification as
{!Pervasives.compare}. Along with the type [t], this function [compare]
allows the module [Char] to be passed as argument to the functors
{!Set.Make} and {!Map.Make}. */
let compare: (t, t) => int

/** The equal function for chars.
@since 4.03.0 */
let equal: (t, t) => bool

/* The following is for system use only. Do not call directly. */

external unsafeChr: int => char = "%identity"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to chrUnsafe

3 changes: 3 additions & 0 deletions src/RescriptCore.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ var $$RegExp;

var $$String;

var Char;

var $$Symbol;

var Type;
Expand Down Expand Up @@ -116,6 +118,7 @@ export {
$$Promise ,
$$RegExp ,
$$String ,
Char ,
$$Symbol ,
Type ,
$$JSON ,
Expand Down
1 change: 1 addition & 0 deletions src/RescriptCore.res
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module Ordering = Core__Ordering
module Promise = Core__Promise
module RegExp = Core__RegExp
module String = Core__String
module Char = Core__Char
module Symbol = Core__Symbol
module Type = Core__Type
module JSON = Core__JSON
Expand Down
Loading