本文整理汇总了Golang中strings.IndexByte函数的典型用法代码示例。如果您正苦于以下问题:Golang IndexByte函数的具体用法?Golang IndexByte怎么用?Golang IndexByte使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了IndexByte函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: New
func New(str string) JID {
// TODO(skriptble): Implement RFC7622
var local, domain, resource string
domain = str
slash := strings.IndexByte(domain, '/')
if slash != -1 {
resource = domain[slash+1:]
domain = domain[:slash]
}
at := strings.IndexByte(domain, '@')
if at != -1 {
local = domain[:at]
domain = domain[at+1:]
}
local = parseLocal(local)
domain = parseDomain(domain)
resource = parseResource(resource)
if len([]byte(local)) > 1024 || len([]byte(domain)) > 1024 || len([]byte(resource)) > 1024 {
return Empty
}
return JID{
local: local,
domain: domain,
resource: resource,
}
}
示例2: escape
// escape replaces any characters which are not valid in identifiers with
// corresponding hexadecimal escape sequence (\XX).
func escape(s string) string {
// Check if a replacement is required.
extra := 0
for i := 0; i < len(s); i++ {
if strings.IndexByte(tail, s[i]) == -1 {
// Two extra bytes are required for each invalid byte; e.g.
// "#" -> `\23`
// "世" -> `\E4\B8\96`
extra += 2
}
}
if extra == 0 {
return s
}
// Replace invalid characters.
const hextable = "0123456789ABCDEF"
buf := make([]byte, len(s)+extra)
j := 0
for i := 0; i < len(s); i++ {
b := s[i]
if strings.IndexByte(tail, b) != -1 {
buf[j] = b
j++
continue
}
buf[j] = '\\'
buf[j+1] = hextable[b>>4]
buf[j+2] = hextable[b&0x0F]
j += 3
}
return string(buf)
}
示例3: Unquote
// Unquote interprets s as a single-quoted, double-quoted,
// or backquoted Go string literal, returning the string value
// that s quotes. For example: test=`"\"\n"` (hex: 22 5c 22 5c 6e 22)
// should be converted to `"\n` (hex: 22 0a).
func Unquote(s string) (t string, err error) {
n := len(s)
if n < 2 {
return "", errors.Trace(ErrSyntax)
}
quote := s[0]
if quote != s[n-1] {
return "", errors.Trace(ErrSyntax)
}
s = s[1 : n-1]
if quote != '"' && quote != '\'' {
return "", errors.Trace(ErrSyntax)
}
// Avoid allocation. No need to convert if there is no '\'
if strings.IndexByte(s, '\\') == -1 && strings.IndexByte(s, quote) == -1 {
return s, nil
}
buf := make([]byte, 0, 3*len(s)/2) // Try to avoid more allocations.
for len(s) > 0 {
mb, ss, err := UnquoteChar(s, quote)
if err != nil {
return "", errors.Trace(err)
}
s = ss
buf = append(buf, mb...)
}
return string(buf), nil
}
示例4: main
func main() {
fmt.Println(strings.Index("Hello, world!", "He")) // 0: He가 맨 처음에 있으므로 0
fmt.Println(strings.Index("Hello, world!", "wor")) // 7: wor가 8번째에 있으므로 7
fmt.Println(strings.Index("Hello, world!", "ow")) // -1: ow는 없으므로 -1
fmt.Println(strings.IndexAny("Hello, world!", "eo")) // 1: e가 2번째에 있으므로 1
fmt.Println(strings.IndexAny("Hello, world!", "f")) // -1: f는 없으므로 -1
var c byte
c = 'd'
fmt.Println(strings.IndexByte("Hello, world!", c)) // 11: d가 12번째에 있으므로 11
c = 'f'
fmt.Println(strings.IndexByte("Hello, world!", c)) // -1: f는 없으므로 -1
var r rune
r = '언'
fmt.Println(strings.IndexRune("고 언어", r)) // 4: "언"이 시작되는 인덱스가 4
f := func(r rune) bool {
return unicode.Is(unicode.Hangul, r) // r이 한글 유니코드이면 true를 리턴
}
fmt.Println(strings.IndexFunc("Go 언어", f)) // 3: 한글이 4번째부터 시작하므로 3
fmt.Println(strings.IndexFunc("Go Language", f)) // -1: 한글이 없으므로 -1
fmt.Println(strings.LastIndex("Hello Hello Hello, world!", "Hello"))
// 12: 마지막 Hello가 13번째에 있으므로 12
fmt.Println(strings.LastIndexAny("Hello, world", "ol")) // 10: 마지막 l이 11번째에 있으므로 10
fmt.Println(strings.LastIndexFunc("Go 언어 안녕", f)) // 13: 마지막 한글인 '녕'이 시작되는 인덱스가 13
}
示例5: ParsePrefix
// Parse user information from string
// format: [email protected]
// Taken from: https://github.com/sorcix/irc
// All credit for this function goes to github user sorcix
func ParsePrefix(prefix string) *Prefix {
p := new(Prefix)
user := strings.IndexByte(prefix, '!')
host := strings.IndexByte(prefix, '@')
switch {
case user > 0 && host > user:
p.Name = prefix[:user]
p.User = prefix[user+1 : host]
p.Host = prefix[host+1:]
case user > 0:
p.Name = prefix[:user]
p.User = prefix[user+1:]
case host > 0:
p.Name = prefix[:host]
p.Host = prefix[host+1:]
default:
p.Name = prefix
}
return p
}
示例6: unpackKey
// unpackKey extracts key, index and remainder of the key.
// It returns "", "", "" for error.
func unpackKey(s string) (interface{}, string) {
if s == "" {
return nil, ""
}
if s[0] == '.' {
s = s[1:]
}
dot := strings.IndexByte(s, '.')
open := strings.IndexByte(s, '[')
switch {
case dot == -1 && open == -1:
return s, ""
case dot != -1 && (open == -1 || dot < open):
return s[:dot], s[dot:]
case open != -1 && (dot == -1 || open < dot):
if open > 0 {
return s[:open], s[open:]
}
close := strings.IndexByte(s, ']')
if close == -1 {
return nil, ""
}
n, err := strconv.Atoi(s[1:close])
if err != nil {
return nil, ""
}
return n, s[close+1:]
default: // Cannot happen
return nil, ""
}
}
示例7: QueryToSymlist
/*
* parse a query string to symlist_t struct , string format should be:
* nmq=testmq&mac=xxxx&bootid=xxxx...
*/
func QueryToSymlist(query string) (symlist *SymList, err error) {
var middle int = 0
var end int = 0
data := query
for {
middle = strings.IndexByte(data, '=')
if middle == -1 {
break
}
end = strings.IndexByte(data[middle:], '&')
if end == -1 {
if symlist == nil {
symlist, _ = NewSymlist(data[:middle], data[middle+1:], STRING)
} else {
symlist, _ = AppendSymlistString(symlist, data[:middle], data[middle+1:])
}
break
} else {
if symlist == nil {
symlist, _ = NewSymlist(data[:middle], data[middle+1:middle+end], STRING)
} else {
symlist, _ = AppendSymlistString(symlist, data[:middle], data[middle+1:middle+end])
}
data = data[middle+end+1:]
}
}
return symlist, err
}
示例8: parsePart
func parsePart(part string) (k, v, rest string, err error) {
// `key=value;otherKey=otherValue`
// `key="value";otherKey=otherValue`
idx := strings.IndexByte(part, '=')
if idx < 0 {
err = strconv.ErrSyntax
return
}
k, v = part[0:idx], part[idx+1:]
// k=`key`, v=`value;otherKey=otherValue`
// k=`key`, v=`"value";otherKey=otherValue`
if len(v) > 0 && v[0] == '"' {
v, rest, err = unquote(v)
if err != nil {
return
}
} else {
idx = strings.IndexByte(v, ';')
if idx >= 0 {
v, rest = v[0:idx], v[idx+1:]
}
}
if len(rest) > 0 && rest[0] == ';' {
rest = rest[1:]
}
return
}
示例9: addParamNode
func (n *treenode) addParamNode(pattern string) (*treenode, *stringList, bool) {
i := strings.IndexByte(pattern, byte('{'))
if i >= 0 {
node, paramNames := n.addNode(pattern[:i])
pattern = pattern[i+1:]
i := strings.IndexByte(pattern, byte('}'))
if i < 0 {
panic("invalid route syntax: {" + pattern)
}
nc := strings.Split(pattern[:i], ":") // split {name:constraint}
name := strings.TrimSpace(nc[0])
constraint := ""
if len(nc) > 1 {
constraint = strings.TrimSpace(strings.Join(nc[1:], ""))
}
pn := node.addParamChild(constraint)
paramNames = addItem(paramNames, name)
pattern = pattern[i+1:]
if len(pattern) == 0 {
return pn, paramNames, true
}
node, paramNames = pn.addNode(pattern)
paramNames = addItem(paramNames, name)
return node, paramNames, true
}
return nil, nil, false
}
示例10: substRef
func substRef(pat, repl, str string) string {
if strings.IndexByte(pat, '%') >= 0 && strings.IndexByte(repl, '%') >= 0 {
return substPattern(pat, repl, str)
}
str = strings.TrimSuffix(str, pat)
return str + repl
}
示例11: ServerParseAddr
// ServerParseAddr parses the listening addr and returns this
func ServerParseAddr(listeningAddr string) string {
// check if addr has :port, if not do it +:80 ,we need the hostname for many cases
a := listeningAddr
if a == "" {
// check for os environments
if oshost := os.Getenv("HOST"); oshost != "" {
a = oshost
} else if oshost := os.Getenv("ADDR"); oshost != "" {
a = oshost
} else if osport := os.Getenv("PORT"); osport != "" {
a = ":" + osport
}
if a == "" {
a = DefaultServerAddr
}
}
if portIdx := strings.IndexByte(a, ':'); portIdx == 0 {
// if contains only :port ,then the : is the first letter, so we dont have setted a hostname, lets set it
a = DefaultServerHostname + a
}
if portIdx := strings.IndexByte(a, ':'); portIdx < 0 {
// missing port part, add it
a = a + ":80"
}
return a
}
示例12: parseParams
// Parses the second call's parameters in a stack trace of the form:
//
// goroutine 1 [running]:
// main.printInputs(0x4c4c60, 0x539038)
// /.../go/src/debug/main.go:16 +0xe0
// main.Test1(0x2) <---- parsed
// /.../go/src/debug/main.go:23
//
// Returns the function name and the parameter values, e.g.:
//
// ("main.Test1", [0x2])
//
func parseParams(st string) (string, []uintptr) {
line := 1
start, stop := 0, 0
for i, c := range st {
if c == '\n' {
line++
}
if line == 4 && c == '\n' {
start = i + 1
}
if line == 5 && c == '\n' {
stop = i
}
}
call := st[start:stop]
fname := call[0:strings.IndexByte(call, '(')]
param := call[strings.IndexByte(call, '(')+1 : strings.IndexByte(call, ')')]
params := strings.Split(param, ", ")
parsedParams := make([]uintptr, len(params))
for i := range params {
iv, err := strconv.ParseInt(params[i], 0, 64)
if err != nil {
panic(err.Error())
}
parsedParams[i] = uintptr(iv)
}
return fname, parsedParams
}
示例13: compare
// compare compares two version strings. compare returns -1 if v1 < v2, 1 if v1
// > v2, 0 otherwise.
//
// Non-numeric segments in either argument are considered equal, so
// compare("1.a", "1.b") == 0, but compare("2.a", "1.b") == 1.
func compare(v1, v2 string) int {
if n := strings.IndexByte(v1, '-'); n != -1 {
v1 = v1[:n]
}
if n := strings.IndexByte(v2, '-'); n != -1 {
v2 = v2[:n]
}
var (
currTab = strings.Split(v1, ".")
otherTab = strings.Split(v2, ".")
)
max := len(currTab)
if len(otherTab) > max {
max = len(otherTab)
}
for i := 0; i < max; i++ {
var currInt, otherInt int
if len(currTab) > i {
currInt, _ = strconv.Atoi(currTab[i])
}
if len(otherTab) > i {
otherInt, _ = strconv.Atoi(otherTab[i])
}
if currInt > otherInt {
return 1
}
if otherInt > currInt {
return -1
}
}
return 0
}
示例14: ParsePrefix
// ParsePrefix takes a string and attempts to create a Prefix struct.
func ParsePrefix(raw string) (p *Prefix) {
p = new(Prefix)
user := strings.IndexByte(raw, prefixUser)
host := strings.IndexByte(raw, prefixHost)
switch {
case user > 0 && host > user:
p.Name = raw[:user]
p.User = raw[user+1 : host]
p.Host = raw[host+1:]
case user > 0:
p.Name = raw[:user]
p.User = raw[user+1:]
case host > 0:
p.Name = raw[:host]
p.Host = raw[host+1:]
default:
p.Name = raw
}
return p
}
示例15: parseToKVPair
func parseToKVPair(s string) (key string, value string, err error) {
re := regexp.MustCompile(`^[[email protected]:/_-]`)
if !re.MatchString(s) {
err = fmt.Errorf("Invalid key-value identity: %s", s)
return
}
colon := strings.IndexByte(s, byte(':'))
atsign := strings.IndexByte(s, byte('@'))
if colon >= 0 {
key = s[0:colon]
value = s[(colon + 1):]
if len(value) >= 2 && value[0:2] == "//" {
value = value[2:]
}
} else if atsign >= 0 {
value = s[0:atsign]
key = s[(atsign + 1):]
} else {
value = s
}
key = strings.ToLower(key)
value = strings.ToLower(value)
return
}