本文整理汇总了Golang中strconv.UnquoteChar函数的典型用法代码示例。如果您正苦于以下问题:Golang UnquoteChar函数的具体用法?Golang UnquoteChar怎么用?Golang UnquoteChar使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了UnquoteChar函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: MakeConst
// MakeConst makes an ideal constant from a literal
// token and the corresponding literal string.
func MakeConst(tok token.Token, lit string) Const {
switch tok {
case token.INT:
var x big.Int
_, ok := x.SetString(lit, 0)
assert(ok)
return Const{&x}
case token.FLOAT:
var y big.Rat
_, ok := y.SetString(lit)
assert(ok)
return Const{&y}
case token.IMAG:
assert(lit[len(lit)-1] == 'i')
var im big.Rat
_, ok := im.SetString(lit[0 : len(lit)-1])
assert(ok)
return Const{cmplx{big.NewRat(0, 1), &im}}
case token.CHAR:
assert(lit[0] == '\'' && lit[len(lit)-1] == '\'')
code, _, _, err := strconv.UnquoteChar(lit[1:len(lit)-1], '\'')
assert(err == nil)
return Const{big.NewInt(int64(code))}
case token.STRING:
s, err := strconv.Unquote(lit)
assert(err == nil)
return Const{s}
}
panic("unreachable")
}
示例2: unquoteChar
func unquoteChar(s string) (uint8, error) {
ef := fmt.Errorf
n := len(s)
if n < 3 {
return 0, ef("invalid char literal")
}
if s[0] != '\'' || s[n-1] != '\'' {
return 0, ef("invalid quoting char literal")
}
s = s[1 : n-1]
ret, multi, tail, err := strconv.UnquoteChar(s, '\'')
if multi {
return 0, ef("multibyte char not allowed")
} else if tail != "" {
return 0, ef("char lit has a tail")
} else if err != nil {
return 0, ef("invalid char literal: %s, %v", s, err)
} else if ret > math.MaxUint8 || ret < 0 {
return 0, ef("invalid char value")
}
return uint8(ret), nil
}
示例3: loadUnicode
func (d *Decoder) loadUnicode() error {
line, _, err := d.r.ReadLine()
if err != nil {
return err
}
sline := string(line)
buf := bytes.Buffer{}
for len(sline) >= 6 {
var r rune
var err error
r, _, sline, err = strconv.UnquoteChar(sline, '\'')
if err != nil {
return err
}
_, err = buf.WriteRune(r)
if err != nil {
return err
}
}
if len(sline) > 0 {
return fmt.Errorf("characters remaining after loadUnicode operation: %s", sline)
}
d.push(buf.String())
return nil
}
示例4: getWordsFromQueryFile
/* Returns Query Words, out of order and without repetitions. */
func getWordsFromQueryFile(file string) []string {
tokenMap := make(map[string]bool)
_, data := ReadFile(file)
lowcaseData := strings.ToLower(string(data))
lines := strings.Split(string(lowcaseData), "\n")
for _, line := range lines {
tokens := strings.Split(line, " ")
if tokens != nil {
for i := 0; i < len(tokens); i++ {
if tokens[i] != "" && tokens[i] != "." {
//To handle the null-charachter case
v, _, _, _ := strconv.UnquoteChar(tokens[i], 0)
if v != 0 {
tokenMap[tokens[i]] = true
}
}
}
}
}
returnArray := make([]string, len(tokenMap))
index := 0
for word := range tokenMap {
returnArray[index] = word
index++
}
return returnArray
}
示例5: readTestSampleToMap
func readTestSampleToMap(file string) map[string]SearchResults {
testQueries := make(map[string]SearchResults)
data, _ := ioutil.ReadFile(file)
lines := strings.Split(string(data), "\r\n")
for _, line := range lines {
tokens := strings.Split(line, " ")
if tokens != nil && tokens[0] != "" {
//To handle the null-charachter case
v, _, _, _ := strconv.UnquoteChar(tokens[0], 0)
if v != 0 {
maplist := make(map[string]bool, len(tokens)-1)
for i := 1; i < len(tokens); i++ {
if tokens[i] != "" {
maplist[tokens[i]] = true
}
}
testQueries[tokens[0]] = maplist
}
}
}
return testQueries
}
示例6: evalBasicLit
func evalBasicLit(ctx *Ctx, lit *BasicLit) (reflect.Value, bool, error) {
switch lit.Kind {
case token.CHAR:
if r, _, tail, err := strconv.UnquoteChar(lit.Value[1:len(lit.Value)-1], '\''); err != nil {
return reflect.Value{}, false, ErrBadBasicLit{at(ctx, lit)}
} else if tail != "" {
// parser.ParseExpr() should raise a syntax error before we get here.
panic("go-interactive: bad char lit " + lit.Value)
} else {
return reflect.ValueOf(r), false, nil
}
case token.STRING:
str, err := strconv.Unquote(string(lit.Value))
return reflect.ValueOf(str), true, err
case token.INT:
i, err := strconv.ParseInt(lit.Value, 0, 0)
return reflect.ValueOf(i), false, err
case token.FLOAT:
f, err := strconv.ParseFloat(lit.Value, 64)
return reflect.ValueOf(f), false, err
case token.IMAG:
f, err := strconv.ParseFloat(lit.Value[:len(lit.Value)-1], 64)
return reflect.ValueOf(complex(0, f)), false, err
default:
return reflect.Value{}, false, errors.New(fmt.Sprintf("BasicLit: Bad token type (%+v)", lit))
}
}
示例7: makeRuneConst
// makeRuneConst returns the int64 code point for the rune literal
// lit. The result is nil if lit is not a correct rune literal.
//
func makeRuneConst(lit string) interface{} {
if n := len(lit); n >= 2 {
if code, _, _, err := strconv.UnquoteChar(lit[1:n-1], '\''); err == nil {
return int64(code)
}
}
return nil
}
示例8: replaceUnicode
// replaceUnicode converts hexadecimal Unicode codepoint notations to a one-rune string.
// It assumes the input string is correctly formatted.
func replaceUnicode(s string) string {
if s[1] == '#' {
r, _ := strconv.ParseInt(s[3:len(s)-1], 16, 32)
return string(r)
}
r, _, _, _ := strconv.UnquoteChar(s, 0)
return string(r)
}
示例9: parseCharacters
func parseCharacters(chars string) []string {
parseSingle := func(s string) (r rune, tail string, escaped bool) {
if s[0] == '\\' {
if s[1] == 'u' || s[1] == 'U' {
r, _, tail, err := strconv.UnquoteChar(s, 0)
failOnError(err)
return r, tail, false
} else if strings.HasPrefix(s[1:], "&") {
return '&', s[6:], false
}
return rune(s[1]), s[2:], true
} else if strings.HasPrefix(s, """) {
return '"', s[6:], false
}
r, sz := utf8.DecodeRuneInString(s)
return r, s[sz:], false
}
chars = strings.Trim(chars, "[ ]")
list := []string{}
var r, last, end rune
for len(chars) > 0 {
if chars[0] == '{' { // character sequence
buf := []rune{}
for chars = chars[1:]; len(chars) > 0; {
r, chars, _ = parseSingle(chars)
if r == '}' {
break
}
if r == ' ' {
log.Fatalf("space not supported in sequence %q", chars)
}
buf = append(buf, r)
}
list = append(list, string(buf))
last = 0
} else { // single character
escaped := false
r, chars, escaped = parseSingle(chars)
if r != ' ' {
if r == '-' && !escaped {
if last == 0 {
log.Fatal("'-' should be preceded by a character")
}
end, chars, _ = parseSingle(chars)
for ; last <= end; last++ {
list = append(list, string(last))
}
last = 0
} else {
list = append(list, string(r))
last = r
}
}
}
}
return list
}
示例10: main
func main() {
sr := `\"大\\家\\好!\"`
var c rune
var mb bool
var err error
for ; len(sr) > 0; c, mb, sr, err = strconv.UnquoteChar(sr, '"') {
fmt.Println(c, mb, sr, err)
}
}
示例11: unquote
// copied from go source docs: strconv.Unquote
// removed restriction of single quote 1 character length
func unquote(s string) (t string, err error) {
n := len(s)
if n < 2 {
return "", strconv.ErrSyntax
}
quote := s[0]
if quote != s[n-1] {
return "", strconv.ErrSyntax
}
s = s[1 : n-1]
if quote == '`' {
if contains(s, '`') {
return "", strconv.ErrSyntax
}
return s, nil
}
if quote != '"' && quote != '\'' {
return "", strconv.ErrSyntax
}
if contains(s, '\n') {
return "", strconv.ErrSyntax
}
// Is it trivial? Avoid allocation.
if !contains(s, '\\') && !contains(s, quote) {
switch quote {
case '"':
return s, nil
case '\'':
r, size := utf8.DecodeRuneInString(s)
if size == len(s) && (r != utf8.RuneError || size != 1) {
return s, nil
}
}
}
var runeTmp [utf8.UTFMax]byte
buf := make([]byte, 0, 3*len(s)/2) // Try to avoid more allocations.
for len(s) > 0 {
c, multibyte, ss, err := strconv.UnquoteChar(s, quote)
if err != nil {
return "", err
}
s = ss
if c < utf8.RuneSelf || !multibyte {
buf = append(buf, byte(c))
} else {
n := utf8.EncodeRune(runeTmp[:], c)
buf = append(buf, runeTmp[:n]...)
}
}
return string(buf), nil
}
示例12: pushByte
func (p *Compiler) pushByte(lit string) {
v, multibyte, tail, err := strconv.UnquoteChar(lit[1:len(lit)-1], '\'')
if err != nil {
panic("invalid char `" + lit + "`: " + err.Error())
}
if tail != "" || multibyte {
panic("invalid char: " + lit)
}
p.code.Block(exec.Push(byte(v)))
}
示例13: checkBasicLit
func checkBasicLit(ctx *Ctx, lit *ast.BasicLit, env *Env) (*BasicLit, []error) {
aexpr := &BasicLit{BasicLit: lit}
switch lit.Kind {
case token.CHAR:
if r, _, tail, err := strconv.UnquoteChar(lit.Value[1:len(lit.Value)-1], '\''); err != nil {
return aexpr, []error{ErrBadBasicLit{at(ctx, lit)}}
} else if tail != "" {
// parser.ParseExpr() should raise a syntax error before we get here.
panic("go-interactive: bad char lit " + lit.Value)
} else {
aexpr.constValue = constValueOf(NewConstRune(r))
aexpr.knownType = knownType{ConstRune}
return aexpr, nil
}
case token.STRING:
if str, err := strconv.Unquote(string(lit.Value)); err != nil {
return aexpr, []error{ErrBadBasicLit{at(ctx, lit)}}
} else {
aexpr.constValue = constValueOf(str)
aexpr.knownType = knownType{ConstString}
return aexpr, nil
}
case token.INT:
if i, ok := NewConstInteger(lit.Value); !ok {
return aexpr, []error{ErrBadBasicLit{at(ctx, lit)}}
} else {
aexpr.constValue = constValueOf(i)
aexpr.knownType = knownType{ConstInt}
return aexpr, nil
}
case token.FLOAT:
if f, ok := NewConstFloat(lit.Value); !ok {
return aexpr, []error{ErrBadBasicLit{at(ctx, lit)}}
} else {
aexpr.constValue = constValueOf(f)
aexpr.knownType = knownType{ConstFloat}
return aexpr, nil
}
case token.IMAG:
if i, ok := NewConstImag(lit.Value); !ok {
return aexpr, []error{ErrBadBasicLit{at(ctx, lit)}}
} else {
aexpr.constValue = constValueOf(i)
aexpr.knownType = knownType{ConstComplex}
return aexpr, nil
}
default:
return aexpr, []error{ErrBadBasicLit{at(ctx, lit)}}
}
}
示例14: unquote
// A better string unquoter that handles unicode sequences. Can't use Go's
// standard unquoter because we need to handle single-quoted strings too.
func unquote(chars []byte) (string, bool) {
if !(chars[0] == '"' || chars[0] == '\'') { // it's not quoted
return string(chars), false
}
if len(chars) == 2 { // it's just the quotes
return "", false
}
remainder := string(chars[1 : len(chars)-1])
quotemark := chars[0]
result := make([]rune, 0)
var unquotedRune rune
var err error
if remainder[0] == '\\' && remainder[1] != 'u' {
result = append(result, unEscape(remainder[0:2]))
remainder = remainder[2:len(remainder)]
} else {
unquotedRune, _, remainder, err = strconv.UnquoteChar(remainder, quotemark)
if err != nil {
return "", true
}
result = append(result, unquotedRune)
}
for len(remainder) > 0 {
if remainder[0] == '\\' && remainder[1] != 'u' {
result = append(result, unEscape(remainder[0:2]))
remainder = remainder[2:len(remainder)]
} else {
unquotedRune, _, remainder, err = strconv.UnquoteChar(remainder, quotemark)
if err != nil {
return "", true
}
result = append(result, unquotedRune)
}
}
return string(result), false
}
示例15: compileCharLit
func (a *exprInfo) compileCharLit(lit string) *expr {
if lit[0] != '\'' {
// Caught by parser
a.silentErrors++
return nil
}
v, _, tail, err := strconv.UnquoteChar(lit[1:], '\'')
if err != nil || tail != "'" {
// Caught by parser
a.silentErrors++
return nil
}
return a.compileIdealInt(big.NewInt(int64(v)), "character literal")
}