From e52ed89e52537d0e12a574ba07bc2c28fb2ec9c2 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Wed, 5 Jun 2024 14:27:52 -0300 Subject: [PATCH] add Char module --- src/Core__Char.mjs | 106 +++++++++++++++++++++++++++++++++++++++++++ src/Core__Char.res | 68 +++++++++++++++++++++++++++ src/Core__Char.resi | 57 +++++++++++++++++++++++ src/RescriptCore.mjs | 3 ++ src/RescriptCore.res | 1 + 5 files changed, 235 insertions(+) create mode 100644 src/Core__Char.mjs create mode 100644 src/Core__Char.res create mode 100644 src/Core__Char.resi diff --git a/src/Core__Char.mjs b/src/Core__Char.mjs new file mode 100644 index 00000000..ac72376b --- /dev/null +++ b/src/Core__Char.mjs @@ -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 */ diff --git a/src/Core__Char.res b/src/Core__Char.res new file mode 100644 index 00000000..7f5e76a4 --- /dev/null +++ b/src/Core__Char.res @@ -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 diff --git a/src/Core__Char.resi b/src/Core__Char.resi new file mode 100644 index 00000000..18726beb --- /dev/null +++ b/src/Core__Char.resi @@ -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" diff --git a/src/RescriptCore.mjs b/src/RescriptCore.mjs index aad5ebf3..f021e46c 100644 --- a/src/RescriptCore.mjs +++ b/src/RescriptCore.mjs @@ -36,6 +36,8 @@ var $$RegExp; var $$String; +var Char; + var $$Symbol; var Type; @@ -116,6 +118,7 @@ export { $$Promise , $$RegExp , $$String , + Char , $$Symbol , Type , $$JSON , diff --git a/src/RescriptCore.res b/src/RescriptCore.res index 1e245c47..84731e35 100644 --- a/src/RescriptCore.res +++ b/src/RescriptCore.res @@ -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