func wordWalker()

in codegen/templates/templates.go [449:527]


func wordWalker(str string, f func(*wordInfo)) {
	runes := []rune(strings.TrimFunc(str, isDelimiter))
	w, i, wo := 0, 0, 0 // index of start of word, scan, word offset
	hasCommonInitial := false
	for i+1 <= len(runes) {
		eow := false // whether we hit the end of a word
		switch {
		case i+1 == len(runes):
			eow = true
		case isDelimiter(runes[i+1]):
			// underscore; shift the remainder forward over any run of underscores
			eow = true
			n := 1
			for i+n+1 < len(runes) && isDelimiter(runes[i+n+1]) {
				n++
			}

			// Leave at most one underscore if the underscore is between two digits
			if i+n+1 < len(runes) && unicode.IsDigit(runes[i]) && unicode.IsDigit(runes[i+n+1]) {
				n--
			}

			copy(runes[i+1:], runes[i+n+1:])
			runes = runes[:len(runes)-n]
		case unicode.IsLower(runes[i]) && !unicode.IsLower(runes[i+1]):
			// lower->non-lower
			eow = true
		}
		i++

		initialisms := GetInitialisms()
		// [w,i) is a word.
		word := string(runes[w:i])
		if !eow && initialisms[word] && !unicode.IsLower(runes[i]) {
			// through
			// split IDFoo → ID, Foo
			// but URLs → URLs
		} else if !eow {
			if initialisms[word] {
				hasCommonInitial = true
			}
			continue
		}

		matchCommonInitial := false
		upperWord := strings.ToUpper(word)
		if initialisms[upperWord] {
			// If the uppercase word (string(runes[w:i]) is "ID" or "IP"
			// AND
			// the word is the first two characters of the current word
			// AND
			// that is not the end of the word
			// AND
			// the length of the remaining string is greater than 3
			// AND
			// the third rune is an uppercase one
			// THEN
			// do NOT count this as an initialism.
			switch upperWord {
			case "ID", "IP":
				if remainingRunes := runes[w:]; word == string(remainingRunes[:2]) && !eow && len(remainingRunes) > 3 && unicode.IsUpper(remainingRunes[3]) {
					continue
				}
			}
			hasCommonInitial = true
			matchCommonInitial = true
		}

		f(&wordInfo{
			WordOffset:         wo,
			Word:               word,
			MatchCommonInitial: matchCommonInitial,
			HasCommonInitial:   hasCommonInitial,
		})
		hasCommonInitial = false
		w = i
		wo++
	}
}