github.com/aergoio/aergo@v1.3.1/cmd/brick/context/util.go (about)

     1  package context
     2  
     3  import (
     4  	"strings"
     5  )
     6  
     7  func ParseFirstWord(input string) (string, string) {
     8  
     9  	trimedStr := strings.TrimSpace(input) //remove whitespace
    10  	splitedStr := strings.Fields(trimedStr)
    11  	if len(splitedStr) == 0 {
    12  		return "", ""
    13  	} else if len(splitedStr) == 1 {
    14  		return strings.ToLower(splitedStr[0]), ""
    15  	}
    16  
    17  	return strings.ToLower(splitedStr[0]), strings.Join(splitedStr[1:], " ")
    18  }
    19  
    20  type Chunk struct {
    21  	Accent bool
    22  	Text   string
    23  }
    24  
    25  func SplitSpaceAndAccent(input string, addLastInComplete bool) []Chunk {
    26  
    27  	var ret []Chunk
    28  
    29  	fieldSplit := strings.Fields(input)
    30  	startAccent := -1
    31  
    32  	for i, chunk := range fieldSplit {
    33  		if startAccent == -1 {
    34  			if strings.HasPrefix(chunk, "`") {
    35  				if strings.Count(chunk, "`") > 1 && strings.HasSuffix(chunk, "`") {
    36  					// for example `keyword`
    37  					ret = append(ret, Chunk{Accent: true, Text: chunk[1 : len(chunk)-1]})
    38  				} else {
    39  					// for example `white space`
    40  					startAccent = i
    41  				}
    42  			} else {
    43  				// just normal keyword
    44  				ret = append(ret, Chunk{Accent: false, Text: chunk})
    45  			}
    46  		} else if startAccent != -1 && strings.HasSuffix(chunk, "`") {
    47  
    48  			//end of statement
    49  			mergedStr := strings.Join(fieldSplit[startAccent:i+1], " ")
    50  			ret = append(ret, Chunk{Accent: true, Text: mergedStr[1 : len(mergedStr)-1]})
    51  
    52  			startAccent = -1 // reset
    53  		}
    54  
    55  		// contain last incomplete word
    56  		if addLastInComplete && i+1 == len(fieldSplit) && -1 != startAccent {
    57  			mergedStr := strings.Join(fieldSplit[startAccent:], " ")
    58  			ret = append(ret, Chunk{Accent: true, Text: mergedStr[1:]})
    59  		}
    60  	}
    61  
    62  	return ret
    63  }