aboutsummaryrefslogtreecommitdiff
path: root/lib/strings/string.go
diff options
context:
space:
mode:
authorShulhan <m.shulhan@gmail.com>2020-03-29 04:15:44 +0700
committerShulhan <m.shulhan@gmail.com>2020-03-29 04:15:44 +0700
commitf4912dbb53e00f3aeb34cf26ca6b5d9588ba3533 (patch)
tree87fbb3554d0005975563e53fcbabed2b0cc9ad90 /lib/strings/string.go
parentb886d935c48d82bc445e645921264128d63b2dad (diff)
downloadpakakeh.go-f4912dbb53e00f3aeb34cf26ca6b5d9588ba3533.tar.xz
strings: add function SingleSpace
The SingleSpace function convert all sequences of white spaces into single space ' '.
Diffstat (limited to 'lib/strings/string.go')
-rw-r--r--lib/strings/string.go26
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/strings/string.go b/lib/strings/string.go
index 5eb7d6b7..68aedb9d 100644
--- a/lib/strings/string.go
+++ b/lib/strings/string.go
@@ -138,6 +138,32 @@ func Reverse(input string) string {
}
//
+// SingleSpace convert all sequences of white spaces into single space ' '.
+//
+func SingleSpace(in string) string {
+ var isspace bool
+ out := make([]rune, 0, len(in))
+ for _, r := range in {
+ if unicode.IsSpace(r) {
+ if isspace {
+ continue
+ }
+ isspace = true
+ continue
+ }
+ if isspace {
+ out = append(out, ' ')
+ isspace = false
+ }
+ out = append(out, r)
+ }
+ if isspace {
+ out = append(out, ' ')
+ }
+ return string(out)
+}
+
+//
// Split given a text, return all words in text.
//
// A word is any sequence of character which have length equal or greater than