Example #1
0
// MergeSpace merge mutiple space to one, trim determin whether remove space at prefix and suffix
func MergeSpace(s string, trim bool) string {
	space := false

	idx, end := 0, len(s)
	bs := make([]byte, end)
	for i := 0; i < end; i++ {
		if unibyte.IsSpace(s[i]) {
			space = true
		} else {
			if space && (!trim || idx != 0) {
				bs[idx] = ' '
				idx++
			}

			bs[idx] = s[i]
			idx++

			space = false
		}
	}

	if space && !trim {
		bs[idx] = ' '
		idx++
	}

	return string(bs[:idx])
}
Example #2
0
// LastIndexNonSpace find index of last non-space character, if not exist, -1 was returned
func LastIndexNonSpace(s string) int {
	for i := len(s) - 1; i >= 0; i-- {
		if !unibyte.IsSpace(s[i]) {
			return i
		}
	}

	return -1
}
Example #3
0
// IndexNonSpace find index of first non-space character, if not exist, -1 was returned
func IndexNonSpace(s string) int {
	for i := range s {
		if !unibyte.IsSpace(s[i]) {
			return i
		}
	}

	return -1
}
Example #4
0
// RemoveSpace remove all space characters from string by unibyte.IsSpace
func RemoveSpace(s string) string {
	idx, end := 0, len(s)
	bs := make([]byte, end)
	for i := 0; i < end; i++ {
		if !unibyte.IsSpace(s[i]) {
			bs[idx] = s[i]
			idx++
		}
	}

	return string(bs[:idx])
}