TheAlgorithms/Go · error

provided size %d is too small to use to slice string %q of l

Error message

provided size %d is too small to use to slice string %q of len %d

What it means

NewPolybius slices the character alphabet to 'size' characters; if size exceeds len(chars) the slice would go out of range, so the constructor rejects the (size, chars) combination naming both values.

Source

Thrown at cipher/polybius/polybius.go:31

)

// Polybius is struct having size, characters, and key
type Polybius struct {
	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
}

View on GitHub (pinned to 5ba447ec5f)

Solutions

  1. Supply an alphabet at least as long as size (e.g. 25 letters for a 5x5 square)
  2. Lower size to fit the provided chars string
  3. Include 'J' handling or extra symbols to extend a short alphabet
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at cipher/polybius/polybius.go:31 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/2450c180da097ef7. Report an issue: GitHub.