identifier.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. /*
  2. Copyright 2025 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package content
  14. import (
  15. "regexp"
  16. )
  17. const cIdentifierFmt string = "[A-Za-z_][A-Za-z0-9_]*"
  18. const identifierErrMsg string = "a valid C identifier must start with alphabetic character or '_', followed by a string of alphanumeric characters or '_'"
  19. var cIdentifierRegexp = regexp.MustCompile("^" + cIdentifierFmt + "$")
  20. // IsCIdentifier tests for a string that conforms the definition of an identifier
  21. // in C. This checks the format, but not the length.
  22. func IsCIdentifier(value string) []string {
  23. if !cIdentifierRegexp.MatchString(value) {
  24. return []string{RegexError(identifierErrMsg, cIdentifierFmt, "my_name", "MY_NAME", "MyName")}
  25. }
  26. return nil
  27. }