func preprocessString()

in graph/preprocessor.go [172:224]


func preprocessString(alias *Alias, str string) (string, error) {
	// Load Remote/Local alias definitions
	if externalDefinitionErr := alias.loadExternalAlias(); externalDefinitionErr != nil {
		return "", externalDefinitionErr
	}

	alias.loadGlobalAlias()

	// Validate alias definitions
	if improperFormatErr := alias.resolveMapAndValidate(); improperFormatErr != nil {
		return "", improperFormatErr
	}

	var out strings.Builder
	var command strings.Builder
	ongoingCmd := false

	// Search and replace all strings with the directive
	// (sam) we add a placeholder space at the end of the string below
	// to force the state machine to END. We remove it before returning
	// the result to user
	for _, char := range str + " " {
		if ongoingCmd {
			if char == alias.directive && command.Len() == 0 { // Escape Character Triggered
				out.WriteRune(alias.directive)
				ongoingCmd = false
			} else if !isAlphanumeric(char) { // Delineates the end of an alias
				resolvedCommand, commandPresent := alias.AliasMap[command.String()]
				// If command is not found we assume this to be the expect item itself.
				if !commandPresent {
					out.WriteString(string(alias.directive) + command.String() + string(char))
					ongoingCmd = false
					command.Reset()
				} else {
					out.WriteString(resolvedCommand)
					if char != alias.directive {
						ongoingCmd = false
						out.WriteRune(char)
					}
					command.Reset()
				}
			} else {
				command.WriteRune(char)
			}
		} else if char == alias.directive {
			ongoingCmd = true
		} else {
			out.WriteRune(char)
		}
	}

	return strings.TrimSuffix(out.String(), " "), nil
}