當前位置: 首頁>>代碼示例>>Golang>>正文


Golang unicode.ToTitle函數代碼示例

本文整理匯總了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)
}
開發者ID:IanMurray,項目名稱:ark,代碼行數:7,代碼來源:misc.go

示例2: ExampleToTitle

func ExampleToTitle() {
	const ucG = 'g'
	fmt.Printf("%#U\n", unicode.ToTitle(ucG))

	// Output:
	// U+0047 'G'
}
開發者ID:RajibTheKing,項目名稱:gcc,代碼行數:7,代碼來源:example_test.go

示例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)
			}
		})
	}
}
開發者ID:rawlingsj,項目名稱:gofabric8,代碼行數:28,代碼來源:context_test.go

示例4: capitalize

func capitalize(s string) string {
	if s == "" {
		return s
	}
	r, n := utf8.DecodeRuneInString(s)
	return string(unicode.ToTitle(r)) + s[n:]
}
開發者ID:TomHoenderdos,項目名稱:go-sunos,代碼行數:7,代碼來源:main.go

示例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))
		}
	}
}
開發者ID:msoap,項目名稱:html2data,代碼行數:50,代碼來源:context_test.go

示例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
}
開發者ID:ndaniels,項目名稱:io-1,代碼行數:17,代碼來源:fasta.go

示例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)
}
開發者ID:ccherng,項目名稱:gogl,代碼行數:17,代碼來源:util.go

示例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)
}
開發者ID:aubonbeurre,項目名稱:gcc,代碼行數:18,代碼來源:strings.go

示例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)
}
開發者ID:IntegerCompany,項目名稱:linaro-android-gcc,代碼行數:18,代碼來源:bytes.go

示例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)
		}
	}
}
開發者ID:edisonwsk,項目名稱:golang-on-cygwin,代碼行數:19,代碼來源:maketables.go

示例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])
}
開發者ID:rayleyva,項目名稱:ghaml,代碼行數:44,代碼來源:ghaml.go

示例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)
		}
	}
}
開發者ID:2thetop,項目名稱:go,代碼行數:20,代碼來源:maketables.go

示例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))
		}
	}
}
開發者ID:TriangleGo,項目名稱:golang.org,代碼行數:39,代碼來源:context_test.go

示例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)
}
開發者ID:chsc,項目名稱:bin2go,代碼行數:23,代碼來源:bin2go.go

示例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:]
}
開發者ID:fzerorubigd,項目名稱:go-dberror,代碼行數:5,代碼來源:error.go


注:本文中的unicode.ToTitle函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。