...

Source file src/github.com/go-openapi/swag/name_lexem.go

Documentation: github.com/go-openapi/swag

     1  // Copyright 2015 go-swagger maintainers
     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  
    15  package swag
    16  
    17  import (
    18  	"unicode"
    19  	"unicode/utf8"
    20  )
    21  
    22  type (
    23  	lexemKind uint8
    24  
    25  	nameLexem struct {
    26  		original          string
    27  		matchedInitialism string
    28  		kind              lexemKind
    29  	}
    30  )
    31  
    32  const (
    33  	lexemKindCasualName lexemKind = iota
    34  	lexemKindInitialismName
    35  )
    36  
    37  func newInitialismNameLexem(original, matchedInitialism string) nameLexem {
    38  	return nameLexem{
    39  		kind:              lexemKindInitialismName,
    40  		original:          original,
    41  		matchedInitialism: matchedInitialism,
    42  	}
    43  }
    44  
    45  func newCasualNameLexem(original string) nameLexem {
    46  	return nameLexem{
    47  		kind:     lexemKindCasualName,
    48  		original: original,
    49  	}
    50  }
    51  
    52  func (l nameLexem) GetUnsafeGoName() string {
    53  	if l.kind == lexemKindInitialismName {
    54  		return l.matchedInitialism
    55  	}
    56  
    57  	var (
    58  		first rune
    59  		rest  string
    60  	)
    61  
    62  	for i, orig := range l.original {
    63  		if i == 0 {
    64  			first = orig
    65  			continue
    66  		}
    67  
    68  		if i > 0 {
    69  			rest = l.original[i:]
    70  			break
    71  		}
    72  	}
    73  
    74  	if len(l.original) > 1 {
    75  		b := poolOfBuffers.BorrowBuffer(utf8.UTFMax + len(rest))
    76  		defer func() {
    77  			poolOfBuffers.RedeemBuffer(b)
    78  		}()
    79  		b.WriteRune(unicode.ToUpper(first))
    80  		b.WriteString(lower(rest))
    81  		return b.String()
    82  	}
    83  
    84  	return l.original
    85  }
    86  
    87  func (l nameLexem) GetOriginal() string {
    88  	return l.original
    89  }
    90  
    91  func (l nameLexem) IsInitialism() bool {
    92  	return l.kind == lexemKindInitialismName
    93  }
    94  

View as plain text