登录 用户中心() [退出] 后台管理 注册
   
您的位置: 首页 >> CLQ工作室开源代码 >> 主题: go 1.10 以后新增的 strings.builder 可用在 go 1.7.3     [回主站]     [分站链接]
标题
go 1.10 以后新增的 strings.builder 可用在 go 1.7.3
clq
浏览(364) + 2018-12-23 21:51:46 发表 编辑

关键字:

[2018-12-23 21:54:11 最后更新]
go 1.10 以后新增的 strings.builder

严格来说这个并不是我们开源的代码,而直接来自现在最新的 golang 源码。这个类应该是 go 1.10 引进的,不过我们的环境是  go 1.7.3 。看了网友的文章可以知道这个类只有 120 行。
所以尝试了一下的确可以直接用在 go 1.7.3 的环境中,复制到 x:\Go\src\strings 目录下即可.

直接复制于 https://golang.org/src/strings/builder.go

--------------------------------------------------
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package strings

import (
    "unicode/utf8"
    "unsafe"
)

//clq 2018 直接复制于 https://golang.org/src/strings/builder.go?s=1379:1412#L15   也能用应当是 go 1.10 后才有的

// A Builder is used to efficiently build a string using Write methods.
// It minimizes memory copying. The zero value is ready to use.
// Do not copy a non-zero Builder.
type Builder struct {
    addr *Builder // of receiver, to detect copies by value
    buf  []byte
}

// noescape hides a pointer from escape analysis.  noescape is
// the identity function but escape analysis doesn't think the
// output depends on the input. noescape is inlined and currently
// compiles down to zero instructions.
// USE CAREFULLY!
// This was copied from the runtime; see issues 23382 and 7921.
//go:nosplit
func noescape(p unsafe.Pointer) unsafe.Pointer {
    x := uintptr(p)
    return unsafe.Pointer(x ^ 0)
}

func (b *Builder) copyCheck() {
    if b.addr == nil {
        // This hack works around a failing of Go's escape analysis
        // that was causing b to escape and be heap allocated.
        // See issue 23382.
        // TODO: once issue 7921 is fixed, this should be reverted to
        // just "b.addr = b".
        b.addr = (*Builder)(noescape(unsafe.Pointer(b)))
    } else if b.addr != b {
        panic("strings: illegal use of non-zero Builder copied by value")
    }
}

// String returns the accumulated string.
func (b *Builder) String() string {
    return *(*string)(unsafe.Pointer(&b.buf))
}

// Len returns the number of accumulated bytes; b.Len() == len(b.String()).
func (b *Builder) Len() int { return len(b.buf) }

// Reset resets the Builder to be empty.
func (b *Builder) Reset() {
    b.addr = nil
    b.buf = nil
}

// grow copies the buffer to a new, larger buffer so that there are at least n
// bytes of capacity beyond len(b.buf).
func (b *Builder) grow(n int) {
    buf := make([]byte, len(b.buf), 2*cap(b.buf)+n)
    copy(buf, b.buf)
    b.buf = buf
}

// Grow grows b's capacity, if necessary, to guarantee space for
// another n bytes. After Grow(n), at least n bytes can be written to b
// without another allocation. If n is negative, Grow panics.
func (b *Builder) Grow(n int) {
    b.copyCheck()
    if n < 0 {
        panic("strings.Builder.Grow: negative count")
    }
    if cap(b.buf)-len(b.buf) < n {
        b.grow(n)
    }
}

// Write appends the contents of p to b's buffer.
// Write always returns len(p), nil.
func (b *Builder) Write(p []byte) (int, error) {
    b.copyCheck()
    b.buf = append(b.buf, p...)
    return len(p), nil
}

// WriteByte appends the byte c to b's buffer.
// The returned error is always nil.
func (b *Builder) WriteByte(c byte) error {
    b.copyCheck()
    b.buf = append(b.buf, c)
    return nil
}

// WriteRune appends the UTF-8 encoding of Unicode code point r to b's buffer.
// It returns the length of r and a nil error.
func (b *Builder) WriteRune(r rune) (int, error) {
    b.copyCheck()
    if r < utf8.RuneSelf {
        b.buf = append(b.buf, byte(r))
        return 1, nil
    }
    l := len(b.buf)
    if cap(b.buf)-l < utf8.UTFMax {
        b.grow(utf8.UTFMax)
    }
    n := utf8.EncodeRune(b.buf[l:l+utf8.UTFMax], r)
    b.buf = b.buf[:l+n]
    return n, nil
}

// WriteString appends the contents of s to b's buffer.
// It returns the length of s and a nil error.
func (b *Builder) WriteString(s string) (int, error) {
    b.copyCheck()
    b.buf = append(b.buf, s...)
    return len(s), nil
}



clq
2018-12-23 21:54:10 发表 编辑

因为大家都知道的原因,这个网址国内是上不去的,所以我们不得不贴出来。因为我们有一些很重要的类库都用到它了。
参考以下网址:

https://studygolang.com/articles/12796?fr=sidebar
"
与 bytes.Buffer 类似,strings.Builder 也支持 4 类方法将数据写入 builder 中。

func (b *Builder) Write(p []byte) (int, error)
func (b *Builder) WriteByte(c byte) error
func (b *Builder) WriteRune(r rune) (int, error)
func (b *Builder) WriteString(s string) (int, error)

有了它们,用户可以根据输入数据的不同类型(byte 数组,byte, rune 或者 string),选择对应的写入方法。
"


总数:1 页次:1/1 首页 尾页  
总数:1 页次:1/1 首页 尾页  


所在合集/目录



发表评论:
文本/html模式切换 插入图片 文本/html模式切换


附件:



NEWBT官方QQ群1: 276678893
可求档连环画,漫画;询问文本处理大师等软件使用技巧;求档softhub软件下载及使用技巧.
但不可"开车",严禁国家敏感话题,不可求档涉及版权的文档软件.
验证问题说明申请入群原因即可.

Copyright © 2005-2020 clq, All Rights Reserved
版权所有
桂ICP备15002303号-1