本文整理匯總了Golang中unicode.ToTitle函數的典型用法代碼示例。如果您正苦於以下問題:Golang ToTitle函數的具體用法?Golang ToTitle怎麽用?Golang ToTitle使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了ToTitle函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: CapitalizeFirst
func CapitalizeFirst(s string) string {
runes := []rune(s)
runes[0] = unicode.ToTitle(runes[0])
return string(runes)
}
示例2: ExampleToTitle
func ExampleToTitle() {
const ucG = 'g'
fmt.Printf("%#U\n", unicode.ToTitle(ucG))
// Output:
// U+0047 'G'
}
示例3: TestWordBreaks
func TestWordBreaks(t *testing.T) {
for _, tt := range breakTest {
testtext.Run(t, tt, func(t *testing.T) {
parts := strings.Split(tt, "|")
want := ""
for _, s := range parts {
found := false
// This algorithm implements title casing given word breaks
// as defined in the Unicode standard 3.13 R3.
for _, r := range s {
title := unicode.ToTitle(r)
lower := unicode.ToLower(r)
if !found && title != lower {
found = true
want += string(title)
} else {
want += string(lower)
}
}
}
src := strings.Join(parts, "")
got := Title(language.Und).String(src)
if got != want {
t.Errorf("got %q; want %q", got, want)
}
})
}
}
示例4: capitalize
func capitalize(s string) string {
if s == "" {
return s
}
r, n := utf8.DecodeRuneInString(s)
return string(unicode.ToTitle(r)) + s[n:]
}
示例5: TestMapping
func TestMapping(t *testing.T) {
assigned := rangetable.Assigned(UnicodeVersion)
coreVersion := rangetable.Assigned(unicode.Version)
if coreVersion == nil {
coreVersion = assigned
}
apply := func(r rune, f func(c *context) bool) string {
c := contextFromRune(r)
f(c)
return string(c.dst[:c.pDst])
}
for r, tt := range special {
if got, want := apply(r, lower), tt.toLower; got != want {
t.Errorf("lowerSpecial:(%U): got %+q; want %+q", r, got, want)
}
if got, want := apply(r, title), tt.toTitle; got != want {
t.Errorf("titleSpecial:(%U): got %+q; want %+q", r, got, want)
}
if got, want := apply(r, upper), tt.toUpper; got != want {
t.Errorf("upperSpecial:(%U): got %+q; want %+q", r, got, want)
}
}
for r := rune(0); r <= lastRuneForTesting; r++ {
if !unicode.In(r, assigned) || !unicode.In(r, coreVersion) {
continue
}
if rf := unicode.SimpleFold(r); rf == r || !unicode.In(rf, assigned) {
continue
}
if _, ok := special[r]; ok {
continue
}
want := string(unicode.ToLower(r))
if got := apply(r, lower); got != want {
t.Errorf("lower:%q (%U): got %q %U; want %q %U", r, r, got, []rune(got), want, []rune(want))
}
want = string(unicode.ToUpper(r))
if got := apply(r, upper); got != want {
t.Errorf("upper:%q (%U): got %q %U; want %q %U", r, r, got, []rune(got), want, []rune(want))
}
want = string(unicode.ToTitle(r))
if got := apply(r, title); got != want {
t.Errorf("title:%q (%U): got %q %U; want %q %U", r, r, got, []rune(got), want, []rune(want))
}
}
}
示例6: TranslateNormal
// TranslateNormal is the default translator for regular (NOT aligned) FASTA
// files.
func TranslateNormal(b byte) (seq.Residue, bool) {
switch {
case b >= 'a' && b <= 'z':
return seq.Residue(unicode.ToTitle(rune(b))), true
case b >= 'A' && b <= 'Z':
return seq.Residue(b), true
case b == '*':
return 0, true
case b == '-':
return '-', true
case b == '/': // PIR junk. Chain breaks? WTF?
return '-', true
}
return 0, false
}
示例7: GoName
// Converts strings with underscores to Go-like names. e.g.: bla_blub_foo -> BlaBlubFoo
func GoName(n string) string {
prev := '_'
return strings.Map(func(r rune) rune {
if r == '_' {
prev = r
return -1
}
if prev == '_' {
prev = r
return unicode.ToTitle(r)
}
prev = r
return unicode.ToLower(r)
},
n)
}
示例8: Title
// Title returns a copy of the string s with all Unicode letters that begin words
// mapped to their title case.
func Title(s string) string {
// Use a closure here to remember state.
// Hackish but effective. Depends on Map scanning in order and calling
// the closure once per rune.
prev := rune(' ')
return Map(
func(r rune) rune {
if isSeparator(prev) {
prev = r
return unicode.ToTitle(r)
}
prev = r
return r
},
s)
}
示例9: Title
// Title returns a copy of s with all Unicode letters that begin words
// mapped to their title case.
func Title(s []byte) []byte {
// Use a closure here to remember state.
// Hackish but effective. Depends on Map scanning in order and calling
// the closure once per rune.
prev := ' '
return Map(
func(r int) int {
if isSeparator(prev) {
prev = r
return unicode.ToTitle(r)
}
prev = r
return r
},
s)
}
示例10: fullCaseTest
func fullCaseTest() {
for i, c := range chars {
lower := unicode.ToLower(i);
want := caseIt(i, c.lowerCase);
if lower != want {
fmt.Fprintf(os.Stderr, "lower U+%04X should be U+%04X is U+%04X\n", i, want, lower)
}
upper := unicode.ToUpper(i);
want = caseIt(i, c.upperCase);
if upper != want {
fmt.Fprintf(os.Stderr, "upper U+%04X should be U+%04X is U+%04X\n", i, want, upper)
}
title := unicode.ToTitle(i);
want = caseIt(i, c.titleCase);
if title != want {
fmt.Fprintf(os.Stderr, "title U+%04X should be U+%04X is U+%04X\n", i, want, title)
}
}
}
示例11: getProperCase
// Gets a proper case for a string (i.e. capitalised like a proper noun)
func getProperCase(s string) string {
// Generously 'borrowing' from strings.Map
// In the worst case, the string can grow when mapped, making
// things unpleasant. But it's so rare we barge in assuming it's
// fine. It could also shrink but that falls out naturally.
maxbytes := len(s) // length of b
nbytes := 0 // number of bytes encoded in b
var b []byte = make([]byte, maxbytes)
needsCapitalising := true
for _, c := range s {
r := c
// chars to skip and be followed by capitalisation
if r == '.' || r == '-' || r == '_' || r == ' ' {
needsCapitalising = true
continue
}
if needsCapitalising {
r = unicode.ToTitle(r)
needsCapitalising = false
}
if r >= 0 {
wid := 1
if r >= utf8.RuneSelf {
wid = utf8.RuneLen(r)
}
if nbytes+wid > maxbytes {
// Grow the buffer.
maxbytes = maxbytes*2 + utf8.UTFMax
nb := make([]byte, maxbytes)
copy(nb, b[0:nbytes])
b = nb
}
nbytes += utf8.EncodeRune(b[nbytes:maxbytes], r)
}
}
return string(b[0:nbytes])
}
示例12: fullCaseTest
func fullCaseTest() {
for j, c := range chars {
i := rune(j)
lower := unicode.ToLower(i)
want := caseIt(i, c.lowerCase)
if lower != want {
fmt.Fprintf(os.Stderr, "lower %U should be %U is %U\n", i, want, lower)
}
upper := unicode.ToUpper(i)
want = caseIt(i, c.upperCase)
if upper != want {
fmt.Fprintf(os.Stderr, "upper %U should be %U is %U\n", i, want, upper)
}
title := unicode.ToTitle(i)
want = caseIt(i, c.titleCase)
if title != want {
fmt.Fprintf(os.Stderr, "title %U should be %U is %U\n", i, want, title)
}
}
}
示例13: TestMapping
func TestMapping(t *testing.T) {
apply := func(r rune, f func(c *context) bool) string {
c := contextFromRune(r)
f(c)
return string(c.dst[:c.pDst])
}
for r, tt := range special {
if got, want := apply(r, lower), tt.toLower; got != want {
t.Errorf("lowerSpecial:(%U): got %+q; want %+q", r, got, want)
}
if got, want := apply(r, title), tt.toTitle; got != want {
t.Errorf("titleSpecial:(%U): got %+q; want %+q", r, got, want)
}
if got, want := apply(r, upper), tt.toUpper; got != want {
t.Errorf("upperSpecial:(%U): got %+q; want %+q", r, got, want)
}
}
for r := rune(0); r <= lastRuneForTesting; r++ {
if _, ok := special[r]; ok {
continue
}
want := string(unicode.ToLower(r))
if got := apply(r, lower); got != want {
t.Errorf("lower:%q (%U): got %q %U; want %q %U", r, r, got, []rune(got), want, []rune(want))
}
want = string(unicode.ToUpper(r))
if got := apply(r, upper); got != want {
t.Errorf("upper:%q (%U): got %q %U; want %q %U", r, r, got, []rune(got), want, []rune(want))
}
want = string(unicode.ToTitle(r))
if got := apply(r, title); got != want {
t.Errorf("title:%q (%U): got %q %U; want %q %U", r, r, got, []rune(got), want, []rune(want))
}
}
}
示例14: camelCase
func camelCase(s string) string {
camel := false
first := true
return strings.Map(func(r rune) rune {
if unicode.IsLetter(r) || unicode.IsDigit(r) {
if first { // always lower case first rune
first = false
return unicode.ToLower(r)
}
if camel {
camel = false
return unicode.ToTitle(r)
}
return r
}
if !first { // if first runes aren't letters or digits, don't capitalize
camel = true // unknown rune type -> ignore and title case next rune
}
return -1
}, s)
}
示例15: capitalize
// capitalize the first letter in the string
func capitalize(s string) string {
r, size := utf8.DecodeRuneInString(s)
return fmt.Sprintf("%c", unicode.ToTitle(r)) + s[size:]
}