diff options
| author | Shulhan <m.shulhan@gmail.com> | 2020-06-04 00:59:45 +0700 |
|---|---|---|
| committer | Shulhan <m.shulhan@gmail.com> | 2020-06-04 00:59:45 +0700 |
| commit | 47ade84e63eb2fd00c1454107d237ab0b7336af5 (patch) | |
| tree | bfbf907ee6abf937c0a3a7f77e1b34e7fca70942 /lib/bytes/bytes.go | |
| parent | 427251e6f320849022e55510051797a10d4e9d83 (diff) | |
| download | pakakeh.go-47ade84e63eb2fd00c1454107d237ab0b7336af5.tar.xz | |
bytes: add function MergeSpaces
The function MergeSpaces will convert sequences of white space into
single space ' '.
Diffstat (limited to 'lib/bytes/bytes.go')
| -rw-r--r-- | lib/bytes/bytes.go | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/lib/bytes/bytes.go b/lib/bytes/bytes.go index 54a7856a..4753629f 100644 --- a/lib/bytes/bytes.go +++ b/lib/bytes/bytes.go @@ -388,6 +388,29 @@ func Indexes(s []byte, token []byte) (idxs []int) { } // +// MergeSpaces convert sequences of white spaces into single space ' '. +// +func MergeSpaces(in []byte) (out []byte) { + var isSpace bool + for _, c := range in { + if c == ' ' || c == '\t' || c == '\v' || c == '\f' || c == '\n' || c == '\r' { + isSpace = true + continue + } + if isSpace { + out = append(out, ' ') + isSpace = false + } + out = append(out, c) + } + if isSpace { + out = append(out, ' ') + } + + return out +} + +// // SkipAfterToken skip all bytes until matched token is found and return the // index after the token and boolean true. // |
