使用字节缓冲区(bytes.Buffer)而不依赖于字符串、strvconv等方法进行操作。

huangapple go评论110阅读模式
英文:

working a bytes.Buffer without resorting to strings, strvconv, etc

问题

我只会翻译你提供的文本内容,以下是翻译结果:

我只是想在不使用strconv和strings的情况下完成这个操作,但我不擅长只使用字节进行操作:

  1. func rangeSeq(b *bytes.Buffer) ([][]byte, bool) {
  2. q := bytes.Split(b.Bytes(), []byte{SEQ_RANGE})
  3. if len(q) == 2 {
  4. var ret [][]byte
  5. il, lt := string(q[0]), string(q[1])
  6. initial, err := strconv.ParseInt(il, 10, 64)
  7. last, err := strconv.ParseInt(lt, 10, 64)
  8. if err == nil {
  9. if initial < last {
  10. for i := initial; i <= last; i++ {
  11. out := strconv.AppendInt([]byte{}, i, 10)
  12. ret = append(ret, out)
  13. }
  14. }
  15. return ret, true
  16. }
  17. }
  18. return nil, false
  19. }

有什么建议吗?

英文:

I would simply like to do this without resorting to strconv & strings, but I am not proficient working in bytes only:

  1. func rangeSeq(b *bytes.Buffer) ([][]byte, bool) {
  2. q := bytes.Split(b.Bytes(), []byte{SEQ_RANGE})
  3. if len(q) == 2 {
  4. var ret [][]byte
  5. il, lt := string(q[0]), string(q[1])
  6. initial, err := strconv.ParseInt(il, 10, 64)
  7. last, err := strconv.ParseInt(lt, 10, 64)
  8. if err == nil {
  9. if initial &lt; last {
  10. for i := initial; i &lt;= last; i++ {
  11. out := strconv.AppendInt([]byte{}, i, 10)
  12. ret = append(ret, out)
  13. }
  14. }
  15. return ret, true
  16. }
  17. }
  18. return nil, false
  19. }

suggestions?

答案1

得分: 3

strconv.Parse* 函数没有 []byte 的等效版本(参见 issue 2632)。目前,使用 strconv 是处理这个问题最简单的方法。

但是,你忽略了第一个错误,这是一个 bug。你还可以简化一些东西,并使用更常见的习惯用法,即提前返回而不是增加缩进。我还建议返回一个错误,而不是一个布尔值,以提供更多的上下文信息。

  1. func rangeSeq(b *bytes.Buffer) ([][]byte, error) {
  2. q := bytes.Split(b.Bytes(), sep)
  3. if len(q) != 2 {
  4. return nil, fmt.Errorf("invalid value: %s", b)
  5. }
  6. var ret [][]byte
  7. initial, err := strconv.Atoi(string(q[0]))
  8. if err != nil {
  9. return nil, err
  10. }
  11. last, err := strconv.Atoi(string(q[1]))
  12. if err != nil {
  13. return nil, err
  14. }
  15. if initial < last {
  16. for i := initial; i <= last; i++ {
  17. ret = append(ret, strconv.AppendInt(nil, i, 10))
  18. }
  19. }
  20. return ret, nil
  21. }
英文:

There is no []byte equivalent to the strconv.Parse* functions (see issue 2632). Using strconv is currently the easiest way to handle this.

You are ignoring the first error however, which is a bug. You can also shorten a couple things, and use the more common idiom of returning early instead of increasing indentation. I would also return an error, instead of a bool for more contextual information.

  1. func rangeSeq(b *bytes.Buffer) ([][]byte, error) {
  2. q := bytes.Split(b.Bytes(), sep)
  3. if len(q) != 2 {
  4. return nil, fmt.Errorf(&quot;invalid value: %s&quot;, b)
  5. }
  6. var ret [][]byte
  7. initial, err := strconv.Atoi(string(q[0]))
  8. if err != nil {
  9. return nil, err
  10. }
  11. last, err := strconv.Atoi(string(q[1]))
  12. if err != nil {
  13. return nil, err
  14. }
  15. if initial &lt; last {
  16. for i := initial; i &lt;= last; i++ {
  17. ret = append(ret, strconv.AppendInt(nil, i, 10))
  18. }
  19. }
  20. return ret, nil
  21. }

huangapple
  • 本文由 发表于 2015年8月26日 22:41:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/32229680.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定