From ecb6fd62c275af03a07d892313ab3914d81cd40e Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Thu, 23 Nov 2017 10:06:22 +0100 Subject: Added text functions --- UTF8.ml | 2 ++ UTF8.mli | 20 ++++++++++++++++++++ evaluator.ml | 25 +++++++++++++++++++++++-- readme.rst | 19 ++++++++++++++++++- screen.ml | 2 +- 5 files changed, 64 insertions(+), 4 deletions(-) diff --git a/UTF8.ml b/UTF8.ml index fa02040..a955b1e 100755 --- a/UTF8.ml +++ b/UTF8.ml @@ -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 diff --git a/UTF8.mli b/UTF8.mli index f91b1fd..a2e331e 100755 --- a/UTF8.mli +++ b/UTF8.mli @@ -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; diff --git a/readme.rst b/readme.rst index f37c17f..5d213c2 100755 --- a/readme.rst +++ b/readme.rst @@ -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 +================================= ====================================================== diff --git a/screen.ml b/screen.ml index 8279c62..c61efea 100755 --- a/screen.ml +++ b/screen.ml @@ -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 -- cgit v1.2.3