diff --git a/uniseg/graphemes.scm b/uniseg/graphemes.scm index 432249d..c49443c 100644 --- a/uniseg/graphemes.scm +++ b/uniseg/graphemes.scm @@ -1,5 +1,6 @@ (define-module (uniseg graphemes) #:use-module (uniseg graphemes stream) + #:use-module (uniseg graphemes iterator) #:use-module (ice-9 hash-table) #:use-module (ice-9 streams) #:use-module (srfi srfi-41) @@ -14,7 +15,9 @@ grapheme-glyphs-reverse grapheme-state grapheme-string - string->grapheme)) + string->grapheme + string->grapheme-list + char->grapheme)) (define-immutable-record-type (_make-grapheme width delta-width modification? state glyphs-reverse glyphs-promise string-promise) @@ -45,6 +48,22 @@ "Return a lazily-constructed string of the glyphs in the grapheme." (force (_grapheme-string-promise grapheme))) +(define (string->grapheme-list str) + "Get a list of all the final graphemes (not including intermediates) in the provided string" + (define stream (string->grapheme-stream str)) + (let loop ((stream stream)) + (define grapheme (stream-car stream)) + (define next-stream (stream-cdr stream)) + (define have-next? (stream-pair? next-stream)) + (define next-modification? + (and have-next? + (grapheme-modification? (stream-car next-stream)))) + + (cond + (next-modification? (loop next-stream)) + (have-next? (cons grapheme (loop next-stream))) + (else (list grapheme))))) + (define (string->grapheme str) "Reads from `str' until we reach the end of the first grapheme cluster" (define stream (string->grapheme-stream str)) @@ -64,3 +83,7 @@ (if (grapheme-modification? last-grapheme) last-grapheme first-grapheme)) + +(define (char->grapheme char) + "Attempt to convert a char to a grapheme. Returns #f if no valid grapheme is formed from the single character." + ((make-grapheme-iterator) char))