File: util.go

package info (click to toggle)
golang-github-go-openapi-swag 1%3A0.25.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,200 kB
  • sloc: sh: 30; makefile: 3
file content (129 lines) | stat: -rw-r--r-- 2,651 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2015 go-swagger maintainers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package mangling

import (
	"strings"
	"unicode"
	"unicode/utf8"
)

// Removes leading whitespaces
func trim(str string) string { return strings.TrimSpace(str) }

// upper is strings.ToUpper() combined with trim
func upper(str string) string {
	return strings.ToUpper(trim(str))
}

// lower is strings.ToLower() combined with trim
func lower(str string) string {
	return strings.ToLower(trim(str))
}

// isEqualFoldIgnoreSpace is the same as strings.EqualFold, but
// it ignores leading and trailing blank spaces in the compared
// string.
//
// base is assumed to be composed of upper-cased runes, and be already
// trimmed.
//
// This code is heavily inspired from strings.EqualFold.
func isEqualFoldIgnoreSpace(base []rune, str string) bool {
	var i, baseIndex int
	// equivalent to b := []byte(str), but without data copy
	b := hackStringBytes(str)

	for i < len(b) {
		if c := b[i]; c < utf8.RuneSelf {
			// fast path for ASCII
			if c != ' ' && c != '\t' {
				break
			}
			i++

			continue
		}

		// unicode case
		r, size := utf8.DecodeRune(b[i:])
		if !unicode.IsSpace(r) {
			break
		}
		i += size
	}

	if i >= len(b) {
		return len(base) == 0
	}

	for _, baseRune := range base {
		if i >= len(b) {
			break
		}

		if c := b[i]; c < utf8.RuneSelf {
			// single byte rune case (ASCII)
			if baseRune >= utf8.RuneSelf {
				return false
			}

			baseChar := byte(baseRune)
			if c != baseChar && ((c < 'a') || (c > 'z') || (c-'a'+'A' != baseChar)) {
				return false
			}

			baseIndex++
			i++

			continue
		}

		// unicode case
		r, size := utf8.DecodeRune(b[i:])
		if unicode.ToUpper(r) != baseRune {
			return false
		}
		baseIndex++
		i += size
	}

	if baseIndex != len(base) {
		return false
	}

	// all passed: now we should only have blanks
	for i < len(b) {
		if c := b[i]; c < utf8.RuneSelf {
			// fast path for ASCII
			if c != ' ' && c != '\t' {
				return false
			}
			i++

			continue
		}

		// unicode case
		r, size := utf8.DecodeRune(b[i:])
		if !unicode.IsSpace(r) {
			return false
		}

		i += size
	}

	return true
}