From 2e8d70f073a7d4c8da1a5aa3c24c9ca387d246de Mon Sep 17 00:00:00 2001 From: mobotsar Date: Tue, 31 Dec 2024 22:25:55 -0500 Subject: [PATCH 1/9] Update CCEither.mli fixing type in docstring Changed "form OCaml 4.12" to "from OCaml 4.12". --- src/core/CCEither.mli | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/CCEither.mli b/src/core/CCEither.mli index ce6f4e18..167add5f 100644 --- a/src/core/CCEither.mli +++ b/src/core/CCEither.mli @@ -2,7 +2,7 @@ (** Either Monad - Module that is compatible with Either form OCaml 4.12 but can be use with any + Module that is compatible with Either from OCaml 4.12 but can be use with any ocaml version compatible with container @since 3.2 From d8c00f96be6bfb78411e5ee2105568499ffb57b2 Mon Sep 17 00:00:00 2001 From: mobotsar Date: Wed, 1 Jan 2025 00:35:55 -0500 Subject: [PATCH 2/9] Update CCString.ml with take_while, rtake_while Added two functions to the `CCString` module addressing #463 following the style of `CCString.drop_while` and `CCString.rdrop_while`. Corresponding `CCString.mli` changes to follow. --- src/core/CCString.ml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/core/CCString.ml b/src/core/CCString.ml index 260d9d69..240f9e06 100644 --- a/src/core/CCString.ml +++ b/src/core/CCString.ml @@ -585,6 +585,24 @@ let take n s = else s +let take_while p s = + let i = ref 0 in + while !i < String.length s && p (String.unsafe_get s !i) do + incr i + done; + String.sub s 0 !i + +let rtake_while p s = + let s_len_pred = String.length s - 1 in + let i = ref s_len_pred in + while !i >= 0 && p (String.unsafe_get s !i) do + decr i + done ; + if !i < s_len_pred + then String.sub s (!i + 1) (s_len_pred - !i) + else + "" + let drop n s = if n < String.length s then String.sub s n (String.length s - n) From 1498158a4fdd2b94f5ed752db7646a95461ad406 Mon Sep 17 00:00:00 2001 From: mobotsar Date: Wed, 1 Jan 2025 00:47:17 -0500 Subject: [PATCH 3/9] Update CCString.mli for take_while, rtake_while --- src/core/CCString.mli | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/core/CCString.mli b/src/core/CCString.mli index 0e5e7ba0..519c42a5 100644 --- a/src/core/CCString.mli +++ b/src/core/CCString.mli @@ -182,6 +182,14 @@ val take : int -> string -> string (** [take n s] keeps only the [n] first chars of [s]. @since 0.17 *) +val take_while : (char -> bool) -> string -> string +(** [take_while p s] keeps only the longest prefix [t] of [s] such that every + character [c] in [t] satisfies [f c]. *) + +val rtake_while : (char -> bool) -> string -> string +(** [rtake_while p s] keeps only the longest suffix [t] of [s] such that every + character [c] in [t] satisfies [f c]. *) + val drop : int -> string -> string (** [drop n s] removes the [n] first chars of [s]. @since 0.17 *) From 6c8569a7d9ef1c8da1eaf3f2ef57ee20a27b6a39 Mon Sep 17 00:00:00 2001 From: mobotsar Date: Wed, 1 Jan 2025 00:48:58 -0500 Subject: [PATCH 4/9] Update CCString.mli to align parameter names in mli descriptions and implementations. --- src/core/CCString.mli | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/CCString.mli b/src/core/CCString.mli index 519c42a5..3cca9365 100644 --- a/src/core/CCString.mli +++ b/src/core/CCString.mli @@ -184,11 +184,11 @@ val take : int -> string -> string val take_while : (char -> bool) -> string -> string (** [take_while p s] keeps only the longest prefix [t] of [s] such that every - character [c] in [t] satisfies [f c]. *) + character [c] in [t] satisfies [p c]. *) val rtake_while : (char -> bool) -> string -> string (** [rtake_while p s] keeps only the longest suffix [t] of [s] such that every - character [c] in [t] satisfies [f c]. *) + character [c] in [t] satisfies [p c]. *) val drop : int -> string -> string (** [drop n s] removes the [n] first chars of [s]. From 699b370220a8b87d5a948cc5b9dec7b50a19ce91 Mon Sep 17 00:00:00 2001 From: Alexander Lucas Date: Wed, 1 Jan 2025 09:34:06 -0500 Subject: [PATCH 5/9] Updated String and StringLabels interfaces to reflect `take_while`, `rtake_while`. --- src/core/CCString.mli | 6 ++++-- src/core/CCStringLabels.mli | 10 ++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/core/CCString.mli b/src/core/CCString.mli index 3cca9365..e280c381 100644 --- a/src/core/CCString.mli +++ b/src/core/CCString.mli @@ -184,11 +184,13 @@ val take : int -> string -> string val take_while : (char -> bool) -> string -> string (** [take_while p s] keeps only the longest prefix [t] of [s] such that every - character [c] in [t] satisfies [p c]. *) + character [c] in [t] satisfies [p c]. + @since 3.16 *) val rtake_while : (char -> bool) -> string -> string (** [rtake_while p s] keeps only the longest suffix [t] of [s] such that every - character [c] in [t] satisfies [p c]. *) + character [c] in [t] satisfies [p c]. + @since 3.16 *) val drop : int -> string -> string (** [drop n s] removes the [n] first chars of [s]. diff --git a/src/core/CCStringLabels.mli b/src/core/CCStringLabels.mli index 318e8237..a3d4f358 100644 --- a/src/core/CCStringLabels.mli +++ b/src/core/CCStringLabels.mli @@ -193,6 +193,16 @@ val take : int -> string -> string (** [take n s] keeps only the [n] first chars of [s]. @since 0.17 *) +val take_while : p:(char -> bool) -> string -> string +(** [take_while ~p s] keeps only the longest prefix [t] of [s] such that every + character [c] in [t] satisfies [p c]. + @since 3.16 *) + +val rtake_while : p:(char -> bool) -> string -> string +(** [rtake_while ~p s] keeps only the longest suffix [t] of [s] such that every + character [c] in [t] satisfies [p c]. + @since 3.16 *) + val drop : int -> string -> string (** [drop n s] removes the [n] first chars of [s]. @since 0.17 *) From 330cba94de13ef400fac5e9999bad5293af5a708 Mon Sep 17 00:00:00 2001 From: Alexander Lucas Date: Wed, 1 Jan 2025 09:46:22 -0500 Subject: [PATCH 6/9] added tests for `take_while`, `rtake_while`. --- tests/core/t_string.ml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/core/t_string.ml b/tests/core/t_string.ml index 4db9bea5..f1747f1d 100644 --- a/tests/core/t_string.ml +++ b/tests/core/t_string.ml @@ -281,6 +281,18 @@ eq ~printer:CCFun.id "ab\nc\n" (unlines [ "ab"; "c" ]);; q Q.printable_string (fun s -> trim (unlines (lines s)) = trim s);; q Q.printable_string (fun s -> trim (unlines_gen (lines_gen s)) = trim s);; +eq ~printer:CCFun.id "" (take_while (Char.equal 'c') "heloo_cc");; +eq ~printer:CCFun.id "" (take_while (Char.equal 'c') "");; +eq ~printer:CCFun.id "c" (take_while (Char.equal 'c') "c");; +eq ~printer:CCFun.id "ccc" (take_while (Char.equal 'c') "cccujsuy");; +eq ~printer:CCFun.id "THIS" (take_while (fun c -> Char.code c < 91) "THISisNotWHAtIwANTED");; + +eq ~printer:CCFun.id "cc" (rtake_while (Char.equal 'c') "heloo_cc");; +eq ~printer:CCFun.id "" (rtake_while (Char.equal 'c') "");; +eq ~printer:CCFun.id "c" (rtake_while (Char.equal 'c') "c");; +eq ~printer:CCFun.id "" (rtake_while (Char.equal 'c') "cccujsuy");; +eq ~printer:CCFun.id "ANTED" (rtake_while (fun c -> Char.code c < 91) "THISisNotWHAtIwANTED");; + q Q.(small_list small_string) (fun l -> From d29ed7ee72c5adc1621a291f78c1fc15966c18ff Mon Sep 17 00:00:00 2001 From: Alexander Lucas Date: Wed, 1 Jan 2025 09:55:26 -0500 Subject: [PATCH 7/9] Renamed predicate parameter of `take_while`, `rtake_while` from `p` to `f`, aligining it with pre-existing `drop_while`. --- src/core/CCString.ml | 8 ++++---- src/core/CCString.mli | 8 ++++---- src/core/CCStringLabels.mli | 12 ++++++------ 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/core/CCString.ml b/src/core/CCString.ml index 240f9e06..d6bb8c62 100644 --- a/src/core/CCString.ml +++ b/src/core/CCString.ml @@ -585,17 +585,17 @@ let take n s = else s -let take_while p s = +let take_while f s = let i = ref 0 in - while !i < String.length s && p (String.unsafe_get s !i) do + while !i < String.length s && f (String.unsafe_get s !i) do incr i done; String.sub s 0 !i -let rtake_while p s = +let rtake_while f s = let s_len_pred = String.length s - 1 in let i = ref s_len_pred in - while !i >= 0 && p (String.unsafe_get s !i) do + while !i >= 0 && f (String.unsafe_get s !i) do decr i done ; if !i < s_len_pred diff --git a/src/core/CCString.mli b/src/core/CCString.mli index e280c381..9d2a66ee 100644 --- a/src/core/CCString.mli +++ b/src/core/CCString.mli @@ -183,13 +183,13 @@ val take : int -> string -> string @since 0.17 *) val take_while : (char -> bool) -> string -> string -(** [take_while p s] keeps only the longest prefix [t] of [s] such that every - character [c] in [t] satisfies [p c]. +(** [take_while f s] keeps only the longest prefix [t] of [s] such that every + character [c] in [t] satisfies [f c]. @since 3.16 *) val rtake_while : (char -> bool) -> string -> string -(** [rtake_while p s] keeps only the longest suffix [t] of [s] such that every - character [c] in [t] satisfies [p c]. +(** [rtake_while f s] keeps only the longest suffix [t] of [s] such that every + character [c] in [t] satisfies [f c]. @since 3.16 *) val drop : int -> string -> string diff --git a/src/core/CCStringLabels.mli b/src/core/CCStringLabels.mli index a3d4f358..0bd3f1d7 100644 --- a/src/core/CCStringLabels.mli +++ b/src/core/CCStringLabels.mli @@ -193,14 +193,14 @@ val take : int -> string -> string (** [take n s] keeps only the [n] first chars of [s]. @since 0.17 *) -val take_while : p:(char -> bool) -> string -> string -(** [take_while ~p s] keeps only the longest prefix [t] of [s] such that every - character [c] in [t] satisfies [p c]. +val take_while : f:(char -> bool) -> string -> string +(** [take_while ~f s] keeps only the longest prefix [t] of [s] such that every + character [c] in [t] satisfies [f c]. @since 3.16 *) -val rtake_while : p:(char -> bool) -> string -> string -(** [rtake_while ~p s] keeps only the longest suffix [t] of [s] such that every - character [c] in [t] satisfies [p c]. +val rtake_while : f:(char -> bool) -> string -> string +(** [rtake_while ~f s] keeps only the longest suffix [t] of [s] such that every + character [c] in [t] satisfies [f c]. @since 3.16 *) val drop : int -> string -> string From 8bb3801a5231b419d21033f57e6d28bd87acaf05 Mon Sep 17 00:00:00 2001 From: Alexander Lucas Date: Wed, 1 Jan 2025 10:22:42 -0500 Subject: [PATCH 8/9] Fixed formatting of `CCString.rtake_while`. --- src/core/CCString.ml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/CCString.ml b/src/core/CCString.ml index d6bb8c62..ec87f351 100644 --- a/src/core/CCString.ml +++ b/src/core/CCString.ml @@ -597,9 +597,9 @@ let rtake_while f s = let i = ref s_len_pred in while !i >= 0 && f (String.unsafe_get s !i) do decr i - done ; - if !i < s_len_pred - then String.sub s (!i + 1) (s_len_pred - !i) + done; + if !i < s_len_pred then + String.sub s (!i + 1) (s_len_pred - !i) else "" From 1e06423e8759cc29e9c7de84cfab3e177ff2d08d Mon Sep 17 00:00:00 2001 From: Alexander Lucas Date: Wed, 1 Jan 2025 10:33:05 -0500 Subject: [PATCH 9/9] Fixed formatting of `t_string.ml` tests for `take_while`, etc. --- tests/core/t_string.ml | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/core/t_string.ml b/tests/core/t_string.ml index f1747f1d..47f1f00b 100644 --- a/tests/core/t_string.ml +++ b/tests/core/t_string.ml @@ -280,18 +280,23 @@ eq ~printer:CCFun.id "" (unlines []);; eq ~printer:CCFun.id "ab\nc\n" (unlines [ "ab"; "c" ]);; q Q.printable_string (fun s -> trim (unlines (lines s)) = trim s);; q Q.printable_string (fun s -> trim (unlines_gen (lines_gen s)) = trim s);; - eq ~printer:CCFun.id "" (take_while (Char.equal 'c') "heloo_cc");; eq ~printer:CCFun.id "" (take_while (Char.equal 'c') "");; eq ~printer:CCFun.id "c" (take_while (Char.equal 'c') "c");; eq ~printer:CCFun.id "ccc" (take_while (Char.equal 'c') "cccujsuy");; -eq ~printer:CCFun.id "THIS" (take_while (fun c -> Char.code c < 91) "THISisNotWHAtIwANTED");; + +eq ~printer:CCFun.id "THIS" + (take_while (fun c -> Char.code c < 91) "THISisNotWHAtIwANTED") +;; eq ~printer:CCFun.id "cc" (rtake_while (Char.equal 'c') "heloo_cc");; eq ~printer:CCFun.id "" (rtake_while (Char.equal 'c') "");; eq ~printer:CCFun.id "c" (rtake_while (Char.equal 'c') "c");; eq ~printer:CCFun.id "" (rtake_while (Char.equal 'c') "cccujsuy");; -eq ~printer:CCFun.id "ANTED" (rtake_while (fun c -> Char.code c < 91) "THISisNotWHAtIwANTED");; + +eq ~printer:CCFun.id "ANTED" + (rtake_while (fun c -> Char.code c < 91) "THISisNotWHAtIwANTED") +;; q Q.(small_list small_string)