1 Star 0 Fork 0

liboxwz/dep

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
strings.go 1.29 KB
一键复制 编辑 原始数据 按行查看 历史
tamal 提交于 2017-11-03 06:05 . Move gps package out of internal
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gps
import (
"bytes"
"unicode"
"unicode/utf8"
)
// toFold returns a string with the property that strings.EqualFold(s, t) iff
// ToFold(s) == ToFold(t) This lets us test a large set of strings for
// fold-equivalent duplicates without making a quadratic number of calls to
// EqualFold. Note that strings.ToUpper and strings.ToLower do not have the
// desired property in some corner cases.
//
// This is hoisted from toolchain internals: src/cmd/go/internal/str/str.go
func toFold(s string) string {
// Fast path: all ASCII, no upper case.
// Most paths look like this already.
for i := 0; i < len(s); i++ {
c := s[i]
if c >= utf8.RuneSelf || 'A' <= c && c <= 'Z' {
goto Slow
}
}
return s
Slow:
var buf bytes.Buffer
for _, r := range s {
// SimpleFold(x) cycles to the next equivalent rune > x
// or wraps around to smaller values. Iterate until it wraps,
// and we've found the minimum value.
for {
r0 := r
r = unicode.SimpleFold(r0)
if r <= r0 {
break
}
}
// Exception to allow fast path above: A-Z => a-z
if 'A' <= r && r <= 'Z' {
r += 'a' - 'A'
}
buf.WriteRune(r)
}
return buf.String()
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/liboxwz/dep.git
git@gitee.com:liboxwz/dep.git
liboxwz
dep
dep
v0.4.1

搜索帮助