github.com/blend/go-sdk@v1.20220411.3/stringutil/split_space.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package stringutil
     9  
    10  import "unicode"
    11  
    12  // SplitSpace splits a string on whitespace.
    13  func SplitSpace(text string) (output []string) {
    14  	if len(text) == 0 {
    15  		return
    16  	}
    17  
    18  	var state int
    19  	var word string
    20  	for _, r := range text {
    21  		switch state {
    22  		case 0: // word
    23  			if unicode.IsSpace(r) {
    24  				if len(word) > 0 {
    25  					output = append(output, word)
    26  					word = ""
    27  				}
    28  				state = 1
    29  			} else {
    30  				word = word + string(r)
    31  			}
    32  		case 1:
    33  			if !unicode.IsSpace(r) {
    34  				word = string(r)
    35  				state = 0
    36  			}
    37  		}
    38  	}
    39  
    40  	if len(word) > 0 {
    41  		output = append(output, word)
    42  	}
    43  	return
    44  }