MCPcopy
hub / github.com/nats-io/nats.go / validateSubject

Function validateSubject

nats.go:4390–4419  ·  nats.go::validateSubject

validateSubject checks if the subject contains characters that break the NATS protocol. Uses an adaptive algorithm: manual loop for short subjects (< 16 chars) and SIMD-optimized strings.IndexByte for longer subjects.

(subj string)

Source from the content-addressed store, hash-verified

4388// Uses an adaptive algorithm: manual loop for short subjects (< 16 chars) and
4389// SIMD-optimized strings.IndexByte for longer subjects.
4390func validateSubject(subj string) error {
4391 if subj == "" {
4392 return ErrBadSubject
4393 }
4394
4395 // Adaptive threshold based on benchmark data showing crossover at ~15-20 characters.
4396 const lengthThreshold = 16
4397
4398 if len(subj) < lengthThreshold {
4399 // Fast path for short subjects (< 16 chars)
4400 // Short-circuit on non-control characters.
4401 for i := range len(subj) {
4402 c := subj[i]
4403 if c <= ' ' && (c == ' ' || c == '\t' || c == '\r' || c == '\n') {
4404 return ErrBadSubject
4405 }
4406 }
4407 return nil
4408 }
4409
4410 // Optimized path for long subjects (>= 16 chars)
4411 // Uses SIMD-optimized strings.IndexByte (processes 16+ bytes per instruction)
4412 if strings.IndexByte(subj, ' ') >= 0 ||
4413 strings.IndexByte(subj, '\t') >= 0 ||
4414 strings.IndexByte(subj, '\r') >= 0 ||
4415 strings.IndexByte(subj, '\n') >= 0 {
4416 return ErrBadSubject
4417 }
4418 return nil
4419}
4420
4421// publish is the internal function to publish messages to a nats-server.
4422// Sends a protocol data message by queuing into the bufio writer

Callers 2

TestValidateSubjectFunction · 0.70
publishMethod · 0.70

Calls

no outgoing calls

Tested by 1

TestValidateSubjectFunction · 0.56