Source File
cases.go
Belonging Package
golang.org/x/text/cases
// Copyright 2014 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.
//go:generate go run gen.go gen_trieval.go
// Package cases provides general and language-specific case mappers.
package cases // import "golang.org/x/text/cases"
import (
)
// References:
// - Unicode Reference Manual Chapter 3.13, 4.2, and 5.18.
// - https://www.unicode.org/reports/tr29/
// - https://www.unicode.org/Public/6.3.0/ucd/CaseFolding.txt
// - https://www.unicode.org/Public/6.3.0/ucd/SpecialCasing.txt
// - https://www.unicode.org/Public/6.3.0/ucd/DerivedCoreProperties.txt
// - https://www.unicode.org/Public/6.3.0/ucd/auxiliary/WordBreakProperty.txt
// - https://www.unicode.org/Public/6.3.0/ucd/auxiliary/WordBreakTest.txt
// - http://userguide.icu-project.org/transforms/casemappings
// TODO:
// - Case folding
// - Wide and Narrow?
// - Segmenter option for title casing.
// - ASCII fast paths
// - Encode Soft-Dotted property within trie somehow.
// A Caser transforms given input to a certain case. It implements
// transform.Transformer.
//
// A Caser may be stateful and should therefore not be shared between
// goroutines.
type Caser struct {
t transform.SpanningTransformer
}
// Bytes returns a new byte slice with the result of converting b to the case
// form implemented by c.
func ( Caser) ( []byte) []byte {
, _, _ = transform.Bytes(.t, )
return
}
// String returns a string with the result of transforming s to the case form
// implemented by c.
func ( Caser) ( string) string {
, _, _ = transform.String(.t, )
return
}
// Reset resets the Caser to be reused for new input after a previous call to
// Transform.
func ( Caser) () { .t.Reset() }
// Transform implements the transform.Transformer interface and transforms the
// given input to the case form implemented by c.
func ( Caser) (, []byte, bool) (, int, error) {
return .t.Transform(, , )
}
// Span implements the transform.SpanningTransformer interface.
func ( Caser) ( []byte, bool) ( int, error) {
return .t.Span(, )
}
// Upper returns a Caser for language-specific uppercasing.
func ( language.Tag, ...Option) Caser {
return Caser{makeUpper(, getOpts(...))}
}
// Lower returns a Caser for language-specific lowercasing.
func ( language.Tag, ...Option) Caser {
return Caser{makeLower(, getOpts(...))}
}
// Title returns a Caser for language-specific title casing. It uses an
// approximation of the default Unicode Word Break algorithm.
func ( language.Tag, ...Option) Caser {
return Caser{makeTitle(, getOpts(...))}
}
// Fold returns a Caser that implements Unicode case folding. The returned Caser
// is stateless and safe to use concurrently by multiple goroutines.
//
// Case folding does not normalize the input and may not preserve a normal form.
// Use the collate or search package for more convenient and linguistically
// sound comparisons. Use golang.org/x/text/secure/precis for string comparisons
// where security aspects are a concern.
func ( ...Option) Caser {
return Caser{makeFold(getOpts(...))}
}
// An Option is used to modify the behavior of a Caser.
type Option func(o options) options
// TODO: consider these options to take a boolean as well, like FinalSigma.
// The advantage of using this approach is that other providers of a lower-case
// algorithm could set different defaults by prefixing a user-provided slice
// of options with their own. This is handy, for instance, for the precis
// package which would override the default to not handle the Greek final sigma.
var (
// NoLower disables the lowercasing of non-leading letters for a title
// caser.
NoLower Option = noLower
// Compact omits mappings in case folding for characters that would grow the
// input. (Unimplemented.)
Compact Option = compact
)
// TODO: option to preserve a normal form, if applicable?
type options struct {
noLower bool
simple bool
// TODO: segmenter, max ignorable, alternative versions, etc.
ignoreFinalSigma bool
}
func getOpts( ...Option) ( options) {
for , := range {
= ()
}
return
}
func noLower( options) options {
.noLower = true
return
}
func compact( options) options {
.simple = true
return
}
// HandleFinalSigma specifies whether the special handling of Greek final sigma
// should be enabled. Unicode prescribes handling the Greek final sigma for all
// locales, but standards like IDNA and PRECIS override this default.
func ( bool) Option {
if {
return handleFinalSigma
}
return ignoreFinalSigma
}
func ignoreFinalSigma( options) options {
.ignoreFinalSigma = true
return
}
func handleFinalSigma( options) options {
.ignoreFinalSigma = false
return
}
![]() |
The pages are generated with Golds v0.6.7. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |