-
Notifications
You must be signed in to change notification settings - Fork 28
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
aspeddro
wants to merge
1
commit into
rescript-lang:main
Choose a base branch
from
aspeddro:add-char-module
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
add Char
module
#229
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename to
chrUnsafe