diff --git a/CHANGELOG.md b/CHANGELOG.md index 4db4967e0b..d503c99bb6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,10 @@ - Fix `Error.fromException`. https://github.com/rescript-lang/rescript/pull/7364 +#### :house: Internal + +- Remove `Stdlib_Char` module for now. https://github.com/rescript-lang/rescript/pull/7367 + # 12.0.0-alpha.10 #### :rocket: New Feature diff --git a/analysis/reanalyze/src/ExnLib.ml b/analysis/reanalyze/src/ExnLib.ml index 255b483942..c9e7393779 100644 --- a/analysis/reanalyze/src/ExnLib.ml +++ b/analysis/reanalyze/src/ExnLib.ml @@ -52,7 +52,6 @@ let raisesLibTable : (Name.t, Exceptions.t) Hashtbl.t = in let stdlibBigInt = [("fromStringExn", [jsExnError])] in let stdlibBool = [("fromStringExn", [invalidArgument])] in - let stdlibChar = [("fromIntExn", [invalidArgument])] in let stdlibError = [("raise", [jsExnError])] in let stdlibExn = [ @@ -143,7 +142,6 @@ let raisesLibTable : (Name.t, Exceptions.t) Hashtbl.t = ("Belt_SetString", beltSet); ("BigInt", stdlibBigInt); ("Bool", stdlibBool); - ("Char", stdlibChar); ("Error", stdlibError); ("Exn", stdlibExn); ("Js.Json", [("parseExn", [jsExnError])]); @@ -164,8 +162,6 @@ let raisesLibTable : (Name.t, Exceptions.t) Hashtbl.t = ("Stdlib.BigInt", stdlibBigInt); ("Stdlib_Bool", stdlibBool); ("Stdlib.Bool", stdlibBool); - ("Stdlib_Char", stdlibChar); - ("Stdlib.Char", stdlibChar); ("Stdlib_Error", stdlibError); ("Stdlib.Error", stdlibError); ("Stdlib_Exn", stdlibExn); diff --git a/lib/es6/Stdlib_Char.js b/lib/es6/Char.js similarity index 71% rename from lib/es6/Stdlib_Char.js rename to lib/es6/Char.js index b721476036..b2a6536046 100644 --- a/lib/es6/Stdlib_Char.js +++ b/lib/es6/Char.js @@ -1,25 +1,6 @@ -function fromIntExn(n) { - if (n < 0 || n > 255) { - throw { - RE_EXN_ID: "Invalid_argument", - _1: "`Char.fromIntExn` expects an integer between 0 and 255", - Error: new Error() - }; - } - return n; -} - -function fromInt(n) { - if (n < 0 || n > 255) { - return; - } else { - return n; - } -} - function escaped(param) { let exit = 0; if (param >= 40) { @@ -73,7 +54,7 @@ function escaped(param) { } } -function toLowerCaseAscii(c) { +function lowercase_ascii(c) { if (c >= /* 'A' */65 && c <= /* 'Z' */90) { return c + 32 | 0; } else { @@ -81,7 +62,7 @@ function toLowerCaseAscii(c) { } } -function toUpperCaseAscii(c) { +function uppercase_ascii(c) { if (c >= /* 'a' */97 && c <= /* 'z' */122) { return c - 32 | 0; } else { @@ -89,17 +70,19 @@ function toUpperCaseAscii(c) { } } -let lowercase_ascii = toLowerCaseAscii; +function compare(c1, c2) { + return c1 - c2 | 0; +} -let uppercase_ascii = toUpperCaseAscii; +function equal(c1, c2) { + return (c1 - c2 | 0) === 0; +} export { escaped, lowercase_ascii, uppercase_ascii, - toLowerCaseAscii, - toUpperCaseAscii, - fromIntExn, - fromInt, + compare, + equal, } /* No side effect */ diff --git a/lib/es6/Stdlib.js b/lib/es6/Stdlib.js index b92fc0a385..02b8cb4a33 100644 --- a/lib/es6/Stdlib.js +++ b/lib/es6/Stdlib.js @@ -12,7 +12,7 @@ function assertEqual(a, b) { RE_EXN_ID: "Assert_failure", _1: [ "Stdlib.res", - 119, + 118, 4 ], Error: new Error() @@ -29,8 +29,6 @@ let $$BigInt; let Bool; -let Char; - let Console; let $$DataView; @@ -123,7 +121,6 @@ export { $$Array, $$BigInt, Bool, - Char, Console, $$DataView, $$Date, diff --git a/lib/js/Stdlib_Char.js b/lib/js/Char.js similarity index 69% rename from lib/js/Stdlib_Char.js rename to lib/js/Char.js index 1babbdfe04..e854a99cd0 100644 --- a/lib/js/Stdlib_Char.js +++ b/lib/js/Char.js @@ -1,25 +1,6 @@ 'use strict'; -function fromIntExn(n) { - if (n < 0 || n > 255) { - throw { - RE_EXN_ID: "Invalid_argument", - _1: "`Char.fromIntExn` expects an integer between 0 and 255", - Error: new Error() - }; - } - return n; -} - -function fromInt(n) { - if (n < 0 || n > 255) { - return; - } else { - return n; - } -} - function escaped(param) { let exit = 0; if (param >= 40) { @@ -73,7 +54,7 @@ function escaped(param) { } } -function toLowerCaseAscii(c) { +function lowercase_ascii(c) { if (c >= /* 'A' */65 && c <= /* 'Z' */90) { return c + 32 | 0; } else { @@ -81,7 +62,7 @@ function toLowerCaseAscii(c) { } } -function toUpperCaseAscii(c) { +function uppercase_ascii(c) { if (c >= /* 'a' */97 && c <= /* 'z' */122) { return c - 32 | 0; } else { @@ -89,15 +70,17 @@ function toUpperCaseAscii(c) { } } -let lowercase_ascii = toLowerCaseAscii; +function compare(c1, c2) { + return c1 - c2 | 0; +} -let uppercase_ascii = toUpperCaseAscii; +function equal(c1, c2) { + return (c1 - c2 | 0) === 0; +} exports.escaped = escaped; exports.lowercase_ascii = lowercase_ascii; exports.uppercase_ascii = uppercase_ascii; -exports.toLowerCaseAscii = toLowerCaseAscii; -exports.toUpperCaseAscii = toUpperCaseAscii; -exports.fromIntExn = fromIntExn; -exports.fromInt = fromInt; +exports.compare = compare; +exports.equal = equal; /* No side effect */ diff --git a/lib/js/Stdlib.js b/lib/js/Stdlib.js index 42b72d9938..ee78df23da 100644 --- a/lib/js/Stdlib.js +++ b/lib/js/Stdlib.js @@ -12,7 +12,7 @@ function assertEqual(a, b) { RE_EXN_ID: "Assert_failure", _1: [ "Stdlib.res", - 119, + 118, 4 ], Error: new Error() @@ -29,8 +29,6 @@ let $$BigInt; let Bool; -let Char; - let Console; let $$DataView; @@ -122,7 +120,6 @@ exports.IntervalId = IntervalId; exports.$$Array = $$Array; exports.$$BigInt = $$BigInt; exports.Bool = Bool; -exports.Char = Char; exports.Console = Console; exports.$$DataView = $$DataView; exports.$$Date = $$Date; diff --git a/packages/artifacts.txt b/packages/artifacts.txt index c63f048884..66f14ed9f9 100644 --- a/packages/artifacts.txt +++ b/packages/artifacts.txt @@ -72,6 +72,7 @@ lib/es6/Belt_internalMapString.js lib/es6/Belt_internalSetBuckets.js lib/es6/Belt_internalSetInt.js lib/es6/Belt_internalSetString.js +lib/es6/Char.js lib/es6/Dom.js lib/es6/Dom_storage.js lib/es6/Dom_storage2.js @@ -149,7 +150,6 @@ lib/es6/Stdlib_BigInt.js lib/es6/Stdlib_BigInt64Array.js lib/es6/Stdlib_BigUint64Array.js lib/es6/Stdlib_Bool.js -lib/es6/Stdlib_Char.js lib/es6/Stdlib_Console.js lib/es6/Stdlib_DataView.js lib/es6/Stdlib_Date.js @@ -244,6 +244,7 @@ lib/js/Belt_internalMapString.js lib/js/Belt_internalSetBuckets.js lib/js/Belt_internalSetInt.js lib/js/Belt_internalSetString.js +lib/js/Char.js lib/js/Dom.js lib/js/Dom_storage.js lib/js/Dom_storage2.js @@ -321,7 +322,6 @@ lib/js/Stdlib_BigInt.js lib/js/Stdlib_BigInt64Array.js lib/js/Stdlib_BigUint64Array.js lib/js/Stdlib_Bool.js -lib/js/Stdlib_Char.js lib/js/Stdlib_Console.js lib/js/Stdlib_DataView.js lib/js/Stdlib_Date.js @@ -621,6 +621,12 @@ lib/ocaml/Belt_internalSetString.cmi lib/ocaml/Belt_internalSetString.cmj lib/ocaml/Belt_internalSetString.cmt lib/ocaml/Belt_internalSetString.res +lib/ocaml/Char.cmi +lib/ocaml/Char.cmj +lib/ocaml/Char.cmt +lib/ocaml/Char.cmti +lib/ocaml/Char.res +lib/ocaml/Char.resi lib/ocaml/Dom.cmi lib/ocaml/Dom.cmj lib/ocaml/Dom.cmt @@ -969,12 +975,6 @@ lib/ocaml/Stdlib_Bool.cmt lib/ocaml/Stdlib_Bool.cmti lib/ocaml/Stdlib_Bool.res lib/ocaml/Stdlib_Bool.resi -lib/ocaml/Stdlib_Char.cmi -lib/ocaml/Stdlib_Char.cmj -lib/ocaml/Stdlib_Char.cmt -lib/ocaml/Stdlib_Char.cmti -lib/ocaml/Stdlib_Char.res -lib/ocaml/Stdlib_Char.resi lib/ocaml/Stdlib_Console.cmi lib/ocaml/Stdlib_Console.cmj lib/ocaml/Stdlib_Console.cmt diff --git a/runtime/Stdlib_Char.res b/runtime/Char.res similarity index 65% rename from runtime/Stdlib_Char.res rename to runtime/Char.res index f846c78a0f..133e63b873 100644 --- a/runtime/Stdlib_Char.res +++ b/runtime/Char.res @@ -1,3 +1,9 @@ +// FIXME: +// This exists for compatibility reason. +// Move this into Pervasives or Core + +// Below is all deprecated and should be removed in v13 + type t = char external code: t => int = "%identity" @@ -6,22 +12,6 @@ external unsafe_chr: int => t = "%identity" external chr: int => t = "%identity" -external fromIntUnsafe: int => t = "%identity" - -let fromIntExn = n => - if n < 0 || n > 255 { - throw(Invalid_argument("`Char.fromIntExn` expects an integer between 0 and 255")) - } else { - fromIntUnsafe(n) - } - -let fromInt = n => - if n < 0 || n > 255 { - None - } else { - Some(fromIntUnsafe(n)) - } - external bytes_create: int => array = "Array" external bytes_unsafe_set: (array<'a>, int, 'a) => unit = "%array_unsafe_set" @@ -51,23 +41,19 @@ let escaped = param => unsafe_to_string(s) } -let toLowerCaseAscii = c => +let lowercase_ascii = c => if c >= 'A' && c <= 'Z' { unsafe_chr(code(c) + 32) } else { c } -let lowercase_ascii = toLowerCaseAscii - -let toUpperCaseAscii = c => +let uppercase_ascii = c => if c >= 'a' && c <= 'z' { unsafe_chr(code(c) - 32) } else { c } -let uppercase_ascii = toUpperCaseAscii - -external equal: (char, char) => bool = "%equal" -external compare: (char, char) => Stdlib_Ordering.t = "%compare" +let compare = (c1, c2) => code(c1) - code(c2) +let equal = (c1: t, c2: t) => compare(c1, c2) == 0 diff --git a/runtime/Char.resi b/runtime/Char.resi new file mode 100644 index 0000000000..3b5c16483c --- /dev/null +++ b/runtime/Char.resi @@ -0,0 +1,55 @@ +// FIXME: +// This exists for compatibility reason. +// Move this into Pervasives or Core + +@@deprecated("Use type `string` and `String` module instead. This will be removed in v13") + +/** Return the ASCII code of the argument. */ +@deprecated("Use type `string` and `String.charCodeAt` instead. This will be removed in v13") +external code: char => int = "%identity" + +/** Return the character with the given ASCII code. + Beware this function is unsafe. */ +@deprecated("Use type `string` and `String.fromCharCode` instead. This will be removed in v13") +external chr: int => char = "%identity" + +/** 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. */ +@deprecated("Use type `string` instead. This will be removed in v13") +let escaped: char => string + +/** Convert the given character to its equivalent lowercase character, + using the US-ASCII character set. + @since 4.03.0 */ +@deprecated("Use type `string` and `String.toLowerCase` instead. This will be removed in v13") +let lowercase_ascii: char => char + +/** Convert the given character to its equivalent uppercase character, + using the US-ASCII character set. + @since 4.03.0 */ +@deprecated("Use type `string` and `String.toUpperCase` instead. This will be removed in v13") +let uppercase_ascii: char => char + +/** An alias for the type of characters. */ +@deprecated("Use type `string` instead. This will be removed in v13") +type t = 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}. */ +@deprecated("Use type `string` and `String.compare` instead. This will be removed in v13") +let compare: (t, t) => int + +/** The equal function for chars. + @since 4.03.0 */ +@deprecated("Use type `string` and `String.equal` instead. This will be removed in v13") +let equal: (t, t) => bool + +/* The following is for system use only. Do not call directly. */ + +@deprecated("Use type `string` and `String.fromCharCode` instead. This will be removed in v13") +external unsafe_chr: int => char = "%identity" diff --git a/runtime/Pervasives.res b/runtime/Pervasives.res index a06f86d6fc..8ad37dc83e 100644 --- a/runtime/Pervasives.res +++ b/runtime/Pervasives.res @@ -244,13 +244,13 @@ external \"++": (string, string) => string = "%string_concat" /* Character operations -- more in module Char */ -@deprecated("Use `Char.code` instead. This will be removed in v13") +@deprecated("Use type `string` and `String.charCodeAt` instead. This will be removed in v13") external int_of_char: char => int = "%identity" -@deprecated("Use `Char.fromIntUnsafe` instead. This will be removed in v13") +@deprecated("Use type `string` and `String.fromCharCode` instead. This will be removed in v13") external unsafe_char_of_int: int => char = "%identity" -@deprecated("Use `Char.fromIntExn` instead. This will be removed in v13") +@deprecated("Use type `string` and `String.fromCharCode` instead. This will be removed in v13") let char_of_int = n => if n < 0 || n > 255 { invalid_arg("char_of_int") diff --git a/runtime/Stdlib.res b/runtime/Stdlib.res index 3a66acbaa1..012570690f 100644 --- a/runtime/Stdlib.res +++ b/runtime/Stdlib.res @@ -3,7 +3,6 @@ include Stdlib_Global module Array = Stdlib_Array module BigInt = Stdlib_BigInt module Bool = Stdlib_Bool -module Char = Stdlib_Char module Console = Stdlib_Console module DataView = Stdlib_DataView module Date = Stdlib_Date diff --git a/runtime/Stdlib_Char.resi b/runtime/Stdlib_Char.resi deleted file mode 100644 index 3943f1d73b..0000000000 --- a/runtime/Stdlib_Char.resi +++ /dev/null @@ -1,92 +0,0 @@ -/*** - Functions for interacting with ASCII char type. - */ - -/** -Type representing an ASCII char. -*/ -type t = char - -/** Return the ASCII code of the argument. */ -external code: char => int = "%identity" - -/** Return the character with the given ASCII code. - Beware this function is unsafe. */ -@deprecated("Use `Char.fromIntExn` instead. This will be removed in v13") -external chr: int => char = "%identity" - -/** 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 */ -@deprecated("Use `Char.toLowerCaseAscii` instead. This will be removed in v13") -let lowercase_ascii: char => char - -/** Convert the given character to its equivalent uppercase character, - using the US-ASCII character set. - @since 4.03.0 */ -@deprecated("Use `Char.toUpperCaseAscii` instead. This will be removed in v13") -let uppercase_ascii: char => char - -/** -Convert the given character to its equivalent lowercase character, -using the US-ASCII character set. - -## Examples - -```rescript -Char.toLowerCaseAscii('A')->assertEqual('a') -Char.toLowerCaseAscii('Z')->assertEqual('z') -Char.toLowerCaseAscii('a')->assertEqual('a') -``` -*/ -let toLowerCaseAscii: char => char - -/** -Convert the given character to its equivalent uppercase character, -using the US-ASCII character set. - -## Examples - -```rescript -Char.toUpperCaseAscii('a')->assertEqual('A') -Char.toUpperCaseAscii('z')->assertEqual('Z') -Char.toUpperCaseAscii('A')->assertEqual('A') -``` - -*/ -let toUpperCaseAscii: char => char - -/** The comparison function for characters. */ -external compare: (char, char) => Stdlib_Ordering.t = "%compare" - -/** The equal function for chars. */ -external equal: (char, char) => bool = "%equal" - -/* The following is for system use only. Do not call directly. */ -@deprecated("Use `Char.fromIntUnsafe` instead. This will be removed in v13") -external unsafe_chr: int => char = "%identity" - -/** -Return the character with the given ASCII code. -Beware this function is unsafe. -*/ -external fromIntUnsafe: int => t = "%identity" - -/** -Return the character with the given ASCII code. -Raise an `invalid_arg` exception if the argument is not a valid ASCII code. -*/ -let fromIntExn: int => t - -/** -Return the character with the given ASCII code. -Return `None` if the argument is not a valid ASCII code. -*/ -let fromInt: int => option diff --git a/tests/tests/src/coercion_module_alias_test.mjs b/tests/tests/src/coercion_module_alias_test.mjs index aa79854769..d8615b6cdd 100644 --- a/tests/tests/src/coercion_module_alias_test.mjs +++ b/tests/tests/src/coercion_module_alias_test.mjs @@ -1,19 +1,19 @@ // Generated by ReScript, PLEASE EDIT WITH CARE +import * as Char from "rescript/lib/es6/Char.js"; import * as Belt_List from "rescript/lib/es6/Belt_List.js"; -import * as Stdlib_Char from "rescript/lib/es6/Stdlib_Char.js"; function l(prim) { console.log(prim); } -let C$p = Stdlib_Char; +let C$p = Char; console.log(66); console.log(66); -let C3 = Stdlib_Char; +let C3 = Char; console.log(66); @@ -24,10 +24,10 @@ function g(x) { } function F(X) { - return Stdlib_Char; + return Char; } -let C4 = Stdlib_Char; +let C4 = Char; console.log(66); @@ -163,7 +163,7 @@ let C; let C$p$p$p = C$p; -let C$p$p = Stdlib_Char; +let C$p$p = Char; function G0(funarg) { let N = { diff --git a/tests/tests/src/ocaml_compat/Ocaml_String.mjs b/tests/tests/src/ocaml_compat/Ocaml_String.mjs index d15f3d601c..42bc5b9d05 100644 --- a/tests/tests/src/ocaml_compat/Ocaml_String.mjs +++ b/tests/tests/src/ocaml_compat/Ocaml_String.mjs @@ -1,8 +1,8 @@ // Generated by ReScript, PLEASE EDIT WITH CARE +import * as Char from "rescript/lib/es6/Char.js"; import * as Pervasives from "rescript/lib/es6/Pervasives.js"; import * as Ocaml_Array from "./Ocaml_Array.mjs"; -import * as Stdlib_Char from "rescript/lib/es6/Stdlib_Char.js"; import * as Primitive_exceptions from "rescript/lib/es6/Primitive_exceptions.js"; function apply1(f, bytes) { @@ -83,7 +83,7 @@ function escaped(s) { return s; } let bytes = bos(s); - return Ocaml_Array.map(Stdlib_Char.escaped, bytes).join(""); + return Ocaml_Array.map(Char.escaped, bytes).join(""); } function index_rec(s, lim, _i, c) { @@ -237,22 +237,22 @@ function rcontains_from(s, i, c) { function uppercase_ascii(s) { let bytes = bos(s); - return String.fromCodePoint(...Ocaml_Array.map(Stdlib_Char.uppercase_ascii, bytes)); + return String.fromCodePoint(...Ocaml_Array.map(Char.uppercase_ascii, bytes)); } function lowercase_ascii(s) { let bytes = bos(s); - return String.fromCodePoint(...Ocaml_Array.map(Stdlib_Char.lowercase_ascii, bytes)); + return String.fromCodePoint(...Ocaml_Array.map(Char.lowercase_ascii, bytes)); } function capitalize_ascii(s) { let bytes = bos(s); - return String.fromCodePoint(...apply1(Stdlib_Char.uppercase_ascii, bytes)); + return String.fromCodePoint(...apply1(Char.uppercase_ascii, bytes)); } function uncapitalize_ascii(s) { let bytes = bos(s); - return String.fromCodePoint(...apply1(Stdlib_Char.lowercase_ascii, bytes)); + return String.fromCodePoint(...apply1(Char.lowercase_ascii, bytes)); } function split_on_char(sep, s) { diff --git a/tests/tests/src/ocaml_compat/Ocaml_String.res b/tests/tests/src/ocaml_compat/Ocaml_String.res index dd54f93463..2c0f049e68 100644 --- a/tests/tests/src/ocaml_compat/Ocaml_String.res +++ b/tests/tests/src/ocaml_compat/Ocaml_String.res @@ -11,8 +11,8 @@ type t = string module B = { include Array - let uppercase_ascii = bytes => map(Stdlib.Char.uppercase_ascii, bytes) - let lowercase_ascii = bytes => map(Stdlib.Char.lowercase_ascii, bytes) + let uppercase_ascii = bytes => map(Char.uppercase_ascii, bytes) + let lowercase_ascii = bytes => map(Char.lowercase_ascii, bytes) let apply1 = (f, bytes) => if length(bytes) == 0 { @@ -22,10 +22,10 @@ module B = { unsafe_set(r, 0, f(unsafe_get(bytes, 0))) r } - let capitalize_ascii = bytes => apply1(Stdlib.Char.uppercase_ascii, bytes) - let uncapitalize_ascii = bytes => apply1(Stdlib.Char.lowercase_ascii, bytes) + let capitalize_ascii = bytes => apply1(Char.uppercase_ascii, bytes) + let uncapitalize_ascii = bytes => apply1(Char.lowercase_ascii, bytes) - let escaped = bytes => map(Stdlib.Char.escaped, bytes) + let escaped = bytes => map(Char.escaped, bytes) } @send external join: (array, string) => string = "join" diff --git a/tests/tests/src/test_char.res b/tests/tests/src/test_char.res index 3dc84c52fa..22d0122223 100644 --- a/tests/tests/src/test_char.res +++ b/tests/tests/src/test_char.res @@ -1,4 +1,4 @@ let caml_is_printable = c => { - let code = Stdlib.Char.code(c) + let code = Char.code(c) code > 31 && code < 127 }