You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

states.go 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (c) 2016 Couchbase, Inc.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package camelcase
  15. import (
  16. "unicode"
  17. )
  18. // States codify the classes that the parser recognizes.
  19. type State interface {
  20. // is _sym_ the start character
  21. StartSym(sym rune) bool
  22. // is _sym_ a member of a class.
  23. // peek, the next sym on the tape, can also be used to determine a class.
  24. Member(sym rune, peek *rune) bool
  25. }
  26. type LowerCaseState struct{}
  27. func (s *LowerCaseState) Member(sym rune, peek *rune) bool {
  28. return unicode.IsLower(sym)
  29. }
  30. func (s *LowerCaseState) StartSym(sym rune) bool {
  31. return s.Member(sym, nil)
  32. }
  33. type UpperCaseState struct {
  34. startedCollecting bool // denotes that the start character has been read
  35. collectingUpper bool // denotes if this is a class of all upper case letters
  36. }
  37. func (s *UpperCaseState) Member(sym rune, peek *rune) bool {
  38. if !(unicode.IsLower(sym) || unicode.IsUpper(sym)) {
  39. return false
  40. }
  41. if peek != nil && unicode.IsUpper(sym) && unicode.IsLower(*peek) {
  42. return false
  43. }
  44. if !s.startedCollecting {
  45. // now we have to determine if upper-case letters are collected.
  46. s.startedCollecting = true
  47. s.collectingUpper = unicode.IsUpper(sym)
  48. return true
  49. }
  50. return s.collectingUpper == unicode.IsUpper(sym)
  51. }
  52. func (s *UpperCaseState) StartSym(sym rune) bool {
  53. return unicode.IsUpper(sym)
  54. }
  55. type NumberCaseState struct{}
  56. func (s *NumberCaseState) Member(sym rune, peek *rune) bool {
  57. return unicode.IsNumber(sym)
  58. }
  59. func (s *NumberCaseState) StartSym(sym rune) bool {
  60. return s.Member(sym, nil)
  61. }
  62. type NonAlphaNumericCaseState struct{}
  63. func (s *NonAlphaNumericCaseState) Member(sym rune, peek *rune) bool {
  64. return !unicode.IsLower(sym) && !unicode.IsUpper(sym) && !unicode.IsNumber(sym)
  65. }
  66. func (s *NonAlphaNumericCaseState) StartSym(sym rune) bool {
  67. return s.Member(sym, nil)
  68. }