目录

一、字符串的本质

1.字符串的定义

golangcharacterUTF-8nil
// go/src/builtin/builtin.go
// string is the set of all strings of 8-bit bytes, conventionally but not
// necessarily representing UTF-8-encoded text. A string may be empty, but
// not nil. Values of string type are immutable.
type string string

字符串在本质上是一串字符数组,每个字符在存储时都对应了一个或多个整数,整数是多少取决于字符集的编码方式。

s := "golang"
for i := 0; i < len(s); i++ {
  fmt.Printf("s[%v]: %v\n",i, s[i])
}
// s[0]: 103
// s[1]: 111
// s[2]: 108
// s[3]: 97
// s[4]: 110
// s[5]: 103
stringreflect
// go/src/reflect/value.go
// StringHeader is the runtime representation of a string.
// ...
type StringHeader struct {
    Data uintptr
    Len  int
}
LenData

2.字符串的长度

golangutf8utf8len(s)python
print(len("go语言")) 
# 4
s := "go语言"
fmt.Printf("len(s): %v\n", len(s)) 
// len(s): 8

3.字符与符文

goruneruneint32[]runeutf8.RuneCountInString()
s := "go语言"
fmt.Println(len([]rune(s)))
// 4
count := utf8.RuneCountInString(s)
fmt.Println(count)
// 4
rangerune
s := "go语言"
for _, r := range s {
    fmt.Printf("rune: %v  string: %#U\n", r, r)
}
// rune: 103  unicode: U+0067 'g'
// rune: 111  unicode: U+006F 'o'
// rune: 35821  unicode: U+8BED '语'
// rune: 35328  unicode: U+8A00 '言'

二、字符串的原理

1.字符串的解析

golang
// go/src/cmd/compile/internal/syntax/scanner.go
func (s *scanner) next() {
    ...
    switch s.ch {
    ...
    case '"':
        s.stdString()

    case '`':
        s.rawString()
  ...
string(s.segment())setLlit()kindStringLit
func (s *scanner) stdString() {
    ok := true
    s.nextch()

    for {
        if s.ch == '"' {
            s.nextch()
            break
        }
        ...
        s.nextch()
    }

    s.setLit(StringLit, ok)
}
func (s *scanner) rawString() {
    ok := true
    s.nextch()

    for {
        if s.ch == '`' {
            s.nextch()
            break
        }
        ...
        s.nextch()
    }
  
    s.setLit(StringLit, ok)
}
// setLit sets the scanner state for a recognized _Literal token.
func (s *scanner) setLit(kind LitKind, ok bool) {
    s.nlsemi = true
    s.tok = _Literal
    s.lit = string(s.segment())
    s.bad = !ok
    s.kind = kind
}

2.字符串的拼接

字符串可以通过+进行拼接:

s := "go" + "lang"
"go"+"lang"AddStringExpropOADDSTRNodeList
// go/src/cmd/compile/internal/ir/expr.go
// An AddStringExpr is a string concatenation Expr[0] + Exprs[1] + ... + Expr[len(Expr)-1].
type AddStringExpr struct {
    miniExpr
    List     Nodes
    Prealloc *Name
}
func NewAddStringExpr(pos src.XPos, list []Node) *AddStringExpr {
    n := &AddStringExpr{}
    n.pos = pos
    n.op = OADDSTR
    n.List = list
    return n
}
OpOADDSTRwalkAddString
// go/src/cmd/compile/internal/walk/expr.go
func walkExpr(n ir.Node, init *ir.Nodes) ir.Node {
    ...
    n = walkExpr1(n, init)
    ...
    return n
}
func walkExpr1(n ir.Node, init *ir.Nodes) ir.Node {
    switch n.Op() {
    ...
    case ir.OADDSTR:
        return walkAddString(n.(*ir.AddStringExpr), init)
    ...
    }
    ...
}
walkAddStringcstackBufAddr()
// go/src/cmd/compile/internal/walk/walk.go
const tmpstringbufsize = 32
// go/src/cmd/compile/internal/walk/expr.go
func walkAddString(n *ir.AddStringExpr, init *ir.Nodes) ir.Node {
    c := len(n.List)
    if c < 2 {
            base.Fatalf("walkAddString count %d too small", c)
    }
    buf := typecheck.NodNil()
    if n.Esc() == ir.EscNone {
        sz := int64(0)
        for _, n1 := range n.List {
            if n1.Op() == ir.OLITERAL {
                sz += int64(len(ir.StringVal(n1)))
            }
        }
        // Don't allocate the buffer if the result won't fit.
        if sz < tmpstringbufsize {
            // Create temporary buffer for result string on stack.
            buf = stackBufAddr(tmpstringbufsize, types.Types[types.TUINT8])
            }
	}
	// build list of string arguments
	args := []ir.Node{buf}
	for _, n2 := range n.List {
            args = append(args, typecheck.Conv(n2, types.Types[types.TSTRING]))
	}
	var fn string
	if c <= 5 {
            // small numbers of strings use direct runtime helpers.
            // note: order.expr knows this cutoff too.
            fn = fmt.Sprintf("concatstring%d", c)
	} else {
            // large numbers of strings are passed to the runtime as a slice.
            fn = "concatstrings"

            t := types.NewSlice(types.Types[types.TSTRING])
            // args[1:] to skip buf arg
            slice := ir.NewCompLitExpr(base.Pos, ir.OCOMPLIT, t, args[1:])
            slice.Prealloc = n.Prealloc
            args = []ir.Node{buf, slice}
            slice.SetEsc(ir.EscNone)
	}

	cat := typecheck.LookupRuntime(fn)
	r := ir.NewCallExpr(base.Pos, ir.OCALL, cat, nil)
	r.Args = args
	r1 := typecheck.Expr(r)
	r1 = walkExpr(r1, init)
	r1.SetType(n.Type())
	return r1
}
concatstring1-concatstring5concatstringsslicetypecheck.LookupRuntime(fn)OpOCALLconcatstring1-concatstring5concatstrings
// go/src/runtime/string.go
const tmpStringBufSize = 32
type tmpBuf [tmpStringBufSize]byte
func concatstring2(buf *tmpBuf, a0, a1 string) string {
    return concatstrings(buf, []string{a0, a1})
}
func concatstring3(buf *tmpBuf, a0, a1, a2 string) string {
    return concatstrings(buf, []string{a0, a1, a2})
}
func concatstring4(buf *tmpBuf, a0, a1, a2, a3 string) string {
    return concatstrings(buf, []string{a0, a1, a2, a3})
}
func concatstring5(buf *tmpBuf, a0, a1, a2, a3, a4 string) string {
    return concatstrings(buf, []string{a0, a1, a2, a3, a4})
}
concatstring1-concatstring5slicebytetostringtmprawstringcopy(b,x)b
// concatstrings implements a Go string concatenation x+y+z+...
func concatstrings(buf *tmpBuf, a []string) string {
    ...
    l := 0

    for i, x := range a {
        ...
        n := len(x)
        ...
        l += n
        ...
    }
    s, b := rawstringtmp(buf, l)
    for _, x := range a {
        copy(b, x)
        b = b[len(x):]
    }
    return s
}
func rawstringtmp(buf *tmpBuf, l int) (s string, b []byte) {
    if buf != nil && l <= len(buf) {
        b = buf[:l]
        s = slicebytetostringtmp(&b[0], len(b))
    } else {
        s, b = rawstring(l)
    }
    return
}

func slicebytetostringtmp(ptr *byte, n int) (str string) {
    ...
    stringStructOf(&str).str = unsafe.Pointer(ptr)
    stringStructOf(&str).len = n
    return
}
// rawstring allocates storage for a new string. The returned
// string and byte slice both refer to the same storage.
func rawstring(size int) (s string, b []byte) {
    p := mallocgc(uintptr(size), nil, false)

    stringStructOf(&s).str = p
    stringStructOf(&s).len = size

    *(*slice)(unsafe.Pointer(&b)) = slice{p, size, size}

    return
}

type stringStruct struct {
    str unsafe.Pointer
    len int
}
func stringStructOf(sp *string) *stringStruct {
    return (*stringStruct)(unsafe.Pointer(sp))
}

3.字符串的转换

尽管字符串的底层是字节数组, 但字节数组与字符串的相互转换并不是简单的指针引用,而是涉及了内存复制。当字符串大于32字节时,还需要申请堆内存。

s := "go语言"
b := []byte(s) // stringtoslicebyte
ss := string(b) // slicebytetostring
stringtoslicebytebufrawbyteslice
// go/src/runtime/string.go
func stringtoslicebyte(buf *tmpBuf, s string) []byte {
    var b []byte
    if buf != nil && len(s) <= len(buf) {
        *buf = tmpBuf{}
        b = buf[:len(s)]
    } else {
        b = rawbyteslice(len(s))
    }
    copy(b, s)
    return b
}
func rawbyteslice(size int) (b []byte) {
    cap := roundupsize(uintptr(size))
    p := mallocgc(cap, nil, false)
    if cap != uintptr(size) {
        memclrNoHeapPointers(add(p, uintptr(size)), cap-uintptr(size))
    }

    *(*slice)(unsafe.Pointer(&b)) = slice{p, size, int(cap)}
    return
}
func slicebytetostring(buf *tmpBuf, ptr *byte, n int) (str string) {
    ...
    var p unsafe.Pointer
    if buf != nil && n <= len(buf) {
        p = unsafe.Pointer(buf)
    } else {
        p = mallocgc(uintptr(n), nil, false)
    }
    stringStructOf(&str).str = p
    stringStructOf(&str).len = n
    memmove(p, unsafe.Pointer(ptr), uintptr(n))
    return
}

字节切片转换为字符串时,原理同上。因此字符串和切片的转换涉及内存拷贝,在一些密集转换的场景中,需要评估转换带来的性能损耗。

总结

utf8