golang字符串本質(zhì)與原理詳解
一、字符串的本質(zhì)
1.字符串的定義
golang
中的字符(character
)串指的是所有8比特位字節(jié)字符串的集合,通常(非必須)是UTF-8
編碼的文本。 字符串可以為空,但不能是nil
。 字符串在編譯時即確定了長度,值是不可變的。
// 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
字符串在本質(zhì)上是一串字符數(shù)組,每個字符在存儲時都對應了一個或多個整數(shù),整數(shù)是多少取決于字符集的編碼方式。
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
字符串在編譯時類型為string
,在運行時其類型定義為一個結構體,位于reflect
包中:
// go/src/reflect/value.go // StringHeader is the runtime representation of a string. // ... type StringHeader struct { Data uintptr Len int }
根據(jù)運行時字符串的定義可知,在程序運行的過程中,字符串存儲了長度(Len
)及指向?qū)嶋H數(shù)據(jù)的指針(Data
)。
2.字符串的長度
golang
中所有文件都采用utf8
編碼,字符常量也使用utf8
編碼字符集。1個英文字母占1個字節(jié)長度,一個中文占3個字節(jié)長度。go中對字符串取長度len(s)
指的是字節(jié)長度,而不是字符個數(shù),這與動態(tài)語言如python
中的表現(xiàn)有所差別。如:
print(len("go語言")) # 4
s := "go語言" fmt.Printf("len(s): %v\n", len(s)) // len(s): 8
3.字符與符文
go
中存在一個特殊類型——符文類型(rune
),用來表示和區(qū)分字符串中的字符。rune
的本質(zhì)是int32
。字符串符文的個數(shù)往往才比較符合我們直觀感受上的字符串長度。要計算字符串符文長度,可以先將字符串轉(zhuǎn)為[]rune
類型,或者利用標準庫中的utf8.RuneCountInString()
函數(shù)。
s := "go語言" fmt.Println(len([]rune(s))) // 4 count := utf8.RuneCountInString(s) fmt.Println(count) // 4
當用range
遍歷字符串時,遍歷的就不再是單字節(jié),而是單個符文rune
。
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
在詞法解析階段,通過掃描源代碼,將雙引號和反引號開頭的內(nèi)容分別識別為標準字符串和原始字符串:
// go/src/cmd/compile/internal/syntax/scanner.go func (s *scanner) next() { ... switch s.ch { ... case '"': s.stdString() case '`': s.rawString() ...
然后,不斷的掃描下一個字符,直到遇到另一個雙引號和反引號即結束掃描。并通過string(s.segment())
將解析到的字節(jié)轉(zhuǎn)換為字符串,同時通過setLlit()
方法將掃描到的內(nèi)容類型(kind
)標記為StringLit
。
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"
會被解析為一個字符串相加的表達式(AddStringExpr
)節(jié)點,該表達式的操作op
為OADDSTR
。相加的各部分字符串被解析為節(jié)點Node
列表,并賦給表達式的List
字段:
// 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 }
在構建抽象語法樹時,會遍歷整個語法樹的表達式,在遍歷的過程中,識別到操作Op
的類型為OADDSTR
,則會調(diào)用walkAddString
對字符串加法表達式進行進一步處理:
// 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) ... } ... }
walkAddString
首先計算相加的字符串的個數(shù)c
,如果相加的字符串個數(shù)小于2,則會報錯。接下來會對相加的字符串字節(jié)長度求和,如果字符串總字節(jié)長度小于32,則會通過stackBufAddr()
在棧空間開辟一塊32字節(jié)的緩存空間。否則會在堆區(qū)開辟一個足夠大的內(nèi)存空間,用于存儲多個字符串。
// 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 }
如果用于相加的字符串個數(shù)小于等于5個,則會調(diào)用運行時的字符串拼接concatstring1-concatstring5
函數(shù)。否則調(diào)用運行時的concatstrings
函數(shù),并將字符串通過切片slice
的形式傳入。類型檢查中的typecheck.LookupRuntime(fn)
方法查找到運行時的字符串拼接函數(shù)后,將其構建為一個調(diào)用表達式,操作Op
為OCALL
,最后遍歷調(diào)用表達式完成調(diào)用。concatstring1-concatstring5
中的每一個調(diào)用最終都會調(diào)用concatstrings
函數(shù)。
// 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-concatstring5
已經(jīng)存在一個32字節(jié)的臨時緩存空間供其使用, 并通過slicebytetostringtmp
函數(shù)將該緩存空間的首地址作為字符串的地址,字節(jié)長度作為字符串的長度。如果待拼接字符串的長度大于32字節(jié),則會調(diào)用rawstring
函數(shù),該函數(shù)會在堆區(qū)為字符串分配存儲空間, 并且將該存儲空間的地址指向字符串。由此可以看出,字符串的底層是字節(jié)切片,且指向同一片內(nèi)存區(qū)域。在分配好存儲空間、完成指針指向等工作后,待拼接的字符串切片會被一個一個地通過內(nèi)存拷貝copy(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.字符串的轉(zhuǎn)換
盡管字符串的底層是字節(jié)數(shù)組, 但字節(jié)數(shù)組與字符串的相互轉(zhuǎn)換并不是簡單的指針引用,而是涉及了內(nèi)存復制。當字符串大于32字節(jié)時,還需要申請堆內(nèi)存。
s := "go語言" b := []byte(s) // stringtoslicebyte ss := string(b) // slicebytetostring
當字符串轉(zhuǎn)換為字節(jié)切片時,需要調(diào)用stringtoslicebyte
函數(shù),當字符串小于32字節(jié)時,可以直接使用緩存buf
,但是當字節(jié)長度大于等于32時,rawbyteslice
函數(shù)需要向堆區(qū)申請足夠的內(nèi)存空間,然后通過內(nèi)存復制將字符串拷貝到目標地址。
// 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 }
字節(jié)切片轉(zhuǎn)換為字符串時,原理同上。因此字符串和切片的轉(zhuǎn)換涉及內(nèi)存拷貝,在一些密集轉(zhuǎn)換的場景中,需要評估轉(zhuǎn)換帶來的性能損耗。
總結
- 字符串常量存儲在靜態(tài)存儲區(qū),其內(nèi)容不可以被改變。
- 字符串的本質(zhì)是字符數(shù)組,底層是字節(jié)數(shù)組,且與字符串指向同一個內(nèi)存地址。
- 字符串的長度是字節(jié)長度,要獲取直觀長度,需要先轉(zhuǎn)換為符文數(shù)組,或者通過
utf8
標準庫的方法進行處理。 - 字符串通過掃描源代碼的雙引號和反引號進行解析。
- 字符串常量的拼接發(fā)生在編譯時,且根據(jù)拼接字符串的個數(shù)調(diào)用了對應的運行時拼接函數(shù)。
- 字符串變量的拼接發(fā)生在運行時。
- 無論是字符串的拼接還是轉(zhuǎn)換,當字符串長度小于32字節(jié)時,可以直接使用棧區(qū)32字節(jié)的緩存,反之,需要向堆區(qū)申請足夠的存儲空間。
- 字符串與字節(jié)數(shù)組的相互轉(zhuǎn)換并不是無損的指針引用,涉及到了內(nèi)存復制。在轉(zhuǎn)換密集的場景需要考慮轉(zhuǎn)換的性能和空間損耗。
到此這篇關于golang字符串本質(zhì)與原理詳解的文章就介紹到這了,更多相關golang字符串 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用Gorm操作Oracle數(shù)據(jù)庫踩坑記錄
gorm是目前用得最多的go語言orm庫,本文主要介紹了使用Gorm操作Oracle數(shù)據(jù)庫踩坑記錄,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-06-06VScode下配置Go語言開發(fā)環(huán)境(2023最新)
在VSCode中配置Golang開發(fā)環(huán)境是非常簡單的,本文主要記錄了Go的安裝,以及給vscode配置Go的環(huán)境,具有一定的參考價值,感興趣的可以了解一下2023-10-10golang中為什么Response.Body需要被關閉詳解
這篇文章主要給大家介紹了關于golang中為什么Response.Body需要被關閉的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-08-08