From dc3e31d28123e9de469727bc58998d4892cef4e7 Mon Sep 17 00:00:00 2001 From: Tan Kian-ting Date: Mon, 23 Nov 2026 00:05:20 +0800 Subject: [PATCH] initial commit --- CHANGES | 6 ++++ LICENSE | 7 ++++ README.md | 40 ++++++++++++++++++++++ dune-project | 28 ++++++++++++++++ lib/dune | 4 +++ lib/stringCodepointSplitter.ml | 51 +++++++++++++++++++++++++++++ stringCodepointSplitter.opam | 36 ++++++++++++++++++++ test/dune | 6 ++++ test/stringCodepointSplitter.t | 29 ++++++++++++++++ test/stringCodepointSplitterTest.ml | 4 +++ 10 files changed, 211 insertions(+) create mode 100644 CHANGES create mode 100644 LICENSE create mode 100644 README.md create mode 100644 dune-project create mode 100644 lib/dune create mode 100644 lib/stringCodepointSplitter.ml create mode 100644 stringCodepointSplitter.opam create mode 100644 test/dune create mode 100644 test/stringCodepointSplitter.t create mode 100644 test/stringCodepointSplitterTest.ml diff --git a/CHANGES b/CHANGES new file mode 100644 index 0000000..f28016d --- /dev/null +++ b/CHANGES @@ -0,0 +1,6 @@ +## v0.0.2 (2023-08-30) + - add more tests + - optimalize the list processing + - disable dynamic linking to uutf +## v0.0.1 (2023-08-27) + - add `split_string_by_unicode_codepoint` initially diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..743fab1 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2023 Yoxem & Raphaël Proust + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..a6b6a4e --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +# stringCodepointSplitter + +Split a string to a list of strings of a character by the unicode codepoint. + +It requires module Uutf. + +## Dependencies + - OCaml >= 4.06 + - dune + - uutf + - fildlib + +## Install + +``` +$ cd /path/to/stringCodepointSplitter + +$ dune build + +$ dune install + +``` + +## Generate Docs +``` +$ dune build @doc +``` + +Generated documentations will be in `/path/to/stringCodepointSplitter/_build/default/_doc` in +various formats, incl. html. +## Example + +```OCaml + +let example = "m̄知 who you're." (*don't know who you are*) in + +List.map (fun x -> print_string (x ^ ", ")) (StringCodepointSplitter.split_string_by_unicode_codepoint example);; + +(*it will output : "m, ̄, 知, , w, h, o, , y, o, u, ', r, e, ., "*) +``` diff --git a/dune-project b/dune-project new file mode 100644 index 0000000..c241e25 --- /dev/null +++ b/dune-project @@ -0,0 +1,28 @@ +(lang dune 3.9) +(cram enable) + +(name stringCodepointSplitter) +(version 0.0.2) +(generate_opam_files true) + +(source + (github yoxem/stringCodepointSplitter)) + +(authors "Yoxem ") + +(maintainers "Yoxem ") +(bug_reports https://github.com/Yoxem/stringCodepointSplitter/issues) +(license MIT) + +;(documentation https://github.com/Yoxem/stringCodepointSplitter/) + +(package + (name stringCodepointSplitter) + (synopsis "Split a string to a list of strings of a character by the unicode codepoint") + (description "Split a string to a list of strings of a character by the unicode codepoint. +It requires module Uutf.") + + (depends (ocaml (>= 4.06)) ocamlfind dune uutf) + (tags (string utf8))) + +; See the complete stanza docs at https://dune.readthedocs.io/en/stable/dune-files.html#dune-project diff --git a/lib/dune b/lib/dune new file mode 100644 index 0000000..152dbb8 --- /dev/null +++ b/lib/dune @@ -0,0 +1,4 @@ +(library + (name stringCodepointSplitter) + (public_name stringCodepointSplitter) + (libraries uutf)) diff --git a/lib/stringCodepointSplitter.ml b/lib/stringCodepointSplitter.ml new file mode 100644 index 0000000..37441c0 --- /dev/null +++ b/lib/stringCodepointSplitter.ml @@ -0,0 +1,51 @@ +open Stdlib +(* +(c) 2023 Yoxem (main author) & Raphaël Proust (PR giver) +Under MIT License +*) + +(** + The Module needs [Uutf] Module. + + It only contains [split_string_by_unicode_codepoint], which splits an OCaml string [str] to a [string list] +*) + + +(** Split an OCaml string [str] to a [string list] + +{b Arguments} +{ul + {- [str] the string to be splitted. + }} + + {b Example} + +{[let example= "m̄知 who you're." (*don't know who you are*) in + +List.map (fun x -> print_string (x ^ ", ")) (split_string_by_unicode_codepoint example);; + +(*it will output : "m, ̄, 知, , w, h, o, , y, o, u, ', r, e, ., "*)]} + *) +let split_string_by_unicode_codepoint str = +(*Split a Ocaml string [str] to a `str list` *) + let pred_codepoint = ref (-1) in + let segmented_unit_list = ref [] in + let iterator () y _ = + let () = if !pred_codepoint > -1 then + let current_codepoint = y in + let pred_char_len = current_codepoint - !pred_codepoint in + let unit_substring = Stdlib.String.sub str !pred_codepoint pred_char_len in + segmented_unit_list := unit_substring :: !segmented_unit_list + in + let () = pred_codepoint := y in + () + in + + let _ = Uutf.String.fold_utf_8 iterator () str in + let last_char_len = (Stdlib.String.length str) - !pred_codepoint in + let () = + if last_char_len > 0 then + let unit_substring = Stdlib.String.sub str !pred_codepoint last_char_len in + segmented_unit_list := unit_substring :: !segmented_unit_list + in + List.rev !segmented_unit_list;; diff --git a/stringCodepointSplitter.opam b/stringCodepointSplitter.opam new file mode 100644 index 0000000..601693a --- /dev/null +++ b/stringCodepointSplitter.opam @@ -0,0 +1,36 @@ +# This file is generated by dune, edit dune-project instead +opam-version: "2.0" +version: "0.0.2" +synopsis: + "Split a string to a list of strings of a character by the unicode codepoint" +description: """ +Split a string to a list of strings of a character by the unicode codepoint. +It requires module Uutf.""" +maintainer: ["Yoxem "] +authors: ["Yoxem "] +license: "MIT" +tags: ["string" "utf8"] +homepage: "https://github.com/yoxem/stringCodepointSplitter" +bug-reports: "https://github.com/Yoxem/stringCodepointSplitter/issues" +depends: [ + "ocaml" {>= "4.06"} + "ocamlfind" + "dune" {>= "3.9"} + "uutf" + "odoc" {with-doc} +] +build: [ + ["dune" "subst"] {dev} + [ + "dune" + "build" + "-p" + name + "-j" + jobs + "@install" + "@runtest" {with-test} + "@doc" {with-doc} + ] +] +dev-repo: "git+https://github.com/yoxem/stringCodepointSplitter.git" diff --git a/test/dune b/test/dune new file mode 100644 index 0000000..0caa2b6 --- /dev/null +++ b/test/dune @@ -0,0 +1,6 @@ +(executable + (libraries stringCodepointSplitter) + (name stringCodepointSplitterTest)) + +(cram + (deps ./stringCodepointSplitterTest.exe)) diff --git a/test/stringCodepointSplitter.t b/test/stringCodepointSplitter.t new file mode 100644 index 0000000..5d17abf --- /dev/null +++ b/test/stringCodepointSplitter.t @@ -0,0 +1,29 @@ +ASCII only + $ ./stringCodepointSplitterTest.exe abc + a + b + c + +Still simple but not just ASCII + $ ./stringCodepointSplitterTest.exe «—» + « + — + » + +Example from the docstring of the lib + $ ./stringCodepointSplitterTest.exe "m̄知 who you're." + m + ̄ + 知 + + w + h + o + + y + o + u + ' + r + e + . diff --git a/test/stringCodepointSplitterTest.ml b/test/stringCodepointSplitterTest.ml new file mode 100644 index 0000000..76105fe --- /dev/null +++ b/test/stringCodepointSplitterTest.ml @@ -0,0 +1,4 @@ +let () = + Sys.argv.(1) + |> StringCodepointSplitter.split_string_by_unicode_codepoint + |> List.iter print_endline