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


Golang strconv.UnquoteChar函數代碼示例

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

示例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
}
開發者ID:8l,項目名稱:leaf,代碼行數:25,代碼來源:strconv.go

示例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
}
開發者ID:rtkrruvinskiy,項目名稱:carbon-relay-ng,代碼行數:28,代碼來源:ogórek.go

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

示例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
}
開發者ID:sunrise987,項目名稱:ir,代碼行數:25,代碼來源:precrecal.go

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

示例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
}
開發者ID:strickyak,項目名稱:goterp,代碼行數:11,代碼來源:const.go

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

示例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:], "&amp;") {
				return '&', s[6:], false
			}
			return rune(s[1]), s[2:], true
		} else if strings.HasPrefix(s, "&quot;") {
			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
}
開發者ID:serge-hulne,項目名稱:golang,代碼行數:57,代碼來源:maketables.go

示例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)
	}
}
開發者ID:cwen-coder,項目名稱:study-gopkg,代碼行數:9,代碼來源:UnquoteChar.go

示例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
}
開發者ID:ballacky13,項目名稱:bytengine,代碼行數:56,代碼來源:parser.go

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

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

示例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
}
開發者ID:nmakiya,項目名稱:tritium,代碼行數:40,代碼來源:tokenizer.go

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


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