diff options
author | Sébastien Dailly <sebastien@chimrod.com> | 2017-11-23 10:06:22 +0100 |
---|---|---|
committer | Sébastien Dailly <sebastien@chimrod.com> | 2017-11-24 09:23:35 +0100 |
commit | ecb6fd62c275af03a07d892313ab3914d81cd40e (patch) | |
tree | 52994be2201583b0b7f297347dd4fe98decc0c44 | |
parent | df4778f2f061c71cf1d9e51fe8ec657d04bbdfeb (diff) |
Added text functions
-rwxr-xr-x | UTF8.ml | 2 | ||||
-rwxr-xr-x | UTF8.mli | 20 | ||||
-rwxr-xr-x | evaluator.ml | 25 | ||||
-rwxr-xr-x | readme.rst | 19 | ||||
-rwxr-xr-x | screen.ml | 2 |
5 files changed, 64 insertions, 4 deletions
@@ -21,6 +21,8 @@ let split str ~sep = | [] -> ""
| hd::tl -> hd
+let replace text patt repl = Text.replace text ~patt ~repl
+
module Buffer = struct
include Buffer
@@ -43,6 +43,26 @@ val rev_implode : t list -> t val compare: t -> t -> int
+val replace: t -> t -> t -> t
+
+val upper: t -> t
+
+val lower: t -> t
+
+val code: t -> int
+
+val char: int -> t
+
+val repeat: int -> t -> t
+
+val get: t -> int -> t
+
+val lchop: t -> t
+
+val rchop: t -> t
+
+val sub: t -> int -> int -> t
+
module Buffer : sig
type buffer
diff --git a/evaluator.ml b/evaluator.ml index 2809507..f718e1f 100755 --- a/evaluator.ml +++ b/evaluator.ml @@ -307,11 +307,32 @@ let () = begin register3 "if" (t_bool, t_bool, t_bool) f_bool if_;
register3 "if" (t_bool, t_string, t_string) f_string if_;
- register1 "abs" t_int f_number D.Num.abs;
- register1 "int" t_int f_number D.Num.floor;
+ register1 "abs" t_int f_number D.Num.abs;
+ register1 "int" t_int f_number D.Num.floor;
register1 "rounddown" t_int f_number D.Num.round_down;
register1 "round" t_int f_number D.Num.round;
+ register1 "trim" t_string f_string UTF8.trim;
+ register1 "right" t_string f_string (fun x -> UTF8.get x (-1));
+ register2 "right" (t_string, t_int) f_string (
+ fun t n ->
+ let n' = D.Num.to_int n in
+ UTF8.sub t (-(n')) n'
+ );
+ register1 "left" t_string f_string (fun x -> UTF8.get x 0);
+ register2 "left" (t_string, t_int) f_string (
+ fun t n ->
+ let n' = D.Num.to_int n in
+ UTF8.sub t 0 n'
+ );
+ register1 "len" t_string f_number (fun x -> D.Num.of_int @@ UTF8.length x);
+ register1 "lenb" t_string f_number (fun x -> D.Num.of_int @@ String.length @@ UTF8.to_utf8string x);
+ register1 "lower" t_string f_string UTF8.lower;
+ register1 "unicode" t_string f_number (fun x -> D.Num.of_int @@ UTF8.code x);
+ register1 "unichar" t_int f_string (fun x -> UTF8.char @@ D.Num.to_int x);
+ register1 "upper" t_string f_string UTF8.upper;
+ register3 "substitute" (t_string, t_string, t_string) f_string UTF8.replace;
+ register2 "rept" (t_string, t_int) f_string (fun t n -> UTF8.repeat (D.Num.to_int n) t);
let module CompareBool = Make_Compare(D.Bool) in
CompareBool.register t_bool;
@@ -4,6 +4,7 @@ .. default-role:: code .. contents:: + :depth: 3 =========== Compilation @@ -192,7 +193,7 @@ Function Value *x* `^` *y* Compute *x* ^ *y* `gcd(` *x*; *y* `)` Greatest common divisor `lcm(` *x*; *y* `)` Lowest common multiple -`rnd()` A random number between 0 and 1 +`rand()` A random number between 0 and 1 `sqrt(Numeric)` Square root `exp(Numeric)` Exponential `ln(Numeric)` Natural logarithm @@ -244,3 +245,19 @@ Function Value `atan2(` *x*; *y* `)` Arc tangent of *x* / *y* ===================== ===================================== +Text +---- + +================================= ====================================================== +Function Value +================================= ====================================================== +`trim(Text)` Remove all space on left and right +`upper(Text)` Convert the string to uppercase +`lower(Text)` Convert the string to lowercase +`unicode(Text)` Returns the numeric code for the first Unicode character +`unichar(Numeric)` Converts a code number into a Unicode character or letter +`substitute(Text, Text, Text)` Substitutes new text for old text in a string +`len(Text)` Return the length of a string +`left(Text, Numeric)` Return the first n characters +`right(Text, Numeric)` Return the last n characters +================================= ====================================================== @@ -280,8 +280,8 @@ let editor ?position ?(prefix=UTF8.empty) ?(init=UTF8.empty) t = begin and delete_previous hd = begin let y, x = Curses.getyx t.status in let length = UTF8.length hd in + ignore @@ Curses.wmove t.status y (x - length); for position = 1 to length do - ignore @@ Curses.wmove t.status y (x - position); ignore @@ Curses.wdelch t.status done; end in |