TheAlgorithms/Go · error

provided string %q should only contain latin characters

Error message

provided string %q should only contain latin characters

What it means

Constructor validation error from NewPolybius: while scanning the chars string the code found a rune outside a-z / A-Z; the Latin-square character set may only contain ASCII letters, so any other symbol in the input triggers this error.

Source

Thrown at cipher/polybius/polybius.go:35

	size       int
	characters string
	key        string
}

// NewPolybius returns a pointer to object of Polybius.
// If the size of "chars" is longer than "size",
// "chars" are truncated to "size".
func NewPolybius(key string, size int, chars string) (*Polybius, error) {
	if size < 0 {
		return nil, fmt.Errorf("provided size %d cannot be negative", size)
	}
	key = strings.ToUpper(key)
	if size > len(chars) {
		return nil, fmt.Errorf("provided size %d is too small to use to slice string %q of len %d", size, chars, len(chars))
	}
	for _, r := range chars {
		if (r < 'a' || r > 'z') && (r < 'A' || r > 'Z') {
			return nil, fmt.Errorf("provided string %q should only contain latin characters", chars)
		}
	}
	chars = strings.ToUpper(chars)[:size]
	for i, r := range chars {
		if strings.ContainsRune(chars[i+1:], r) {
			return nil, fmt.Errorf("%q contains same character %q", chars[i+1:], r)
		}
	}

	if len(key) != size*size {
		return nil, fmt.Errorf("len(key): %d must be as long as size squared: %d", len(key), size*size)
	}
	return &Polybius{size, chars, key}, nil
}

// Encrypt encrypts with polybius encryption
func (p *Polybius) Encrypt(text string) (string, error) {
	encryptedText := ""

View on GitHub (pinned to 5ba447ec5f)

Solutions

  1. Strip non-latin characters from the input alphabet before constructing
  2. Map non-latin letters to latin equivalents (transliteration) before encryption
  3. Extend the cipher design if non-latin alphabets must be supported
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at cipher/polybius/polybius.go:35 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02). Data as JSON: /api/errors/f4fa1a300bed0e74. Report an issue: GitHub.