decodeRuneInternal和decodeRuneInStringInternal之间的区别是什么?

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

what's the difference between decodeRuneInternal and decodeRuneInStringInternal

问题

在golang的std包中,"func decodeRuneInternal"和"func decodeRuneInStringInternal"除了参数之外是相同的,即:

func decodeRuneInternal(p []byte) (r rune, size int, short bool)
func decodeRuneInStringInternal(s string) (r rune, size int, short bool)

为什么不直接将decodeRuneInStringInternal定义为:

func decodeRuneInStringInternal(s string) (r rune, size int, short bool) {
    return decodeRuneInternal([]byte(s)) (r rune, size int, short bool)
}

在utf8.go中,decodeRuneInStringInternal的实现与decodeRuneInternal相同。

为什么?

英文:

In golang's std package, "func decodeRuneInternal" and "func decodeRuneInStringInternal" are the same except the args, that is:

func decodeRuneInternal(p []byte) (r rune, size int, short bool)
func decodeRuneInStringInternal(s string) (r rune, size int, short bool)

Why not just define decodeRuneInStringInternal as:

func decodeRuneInStringInternal(s string) (r rune, size int, short bool) {
    return decodeRuneInternal([]byte(s)) (r rune, size int, short bool)
}

in utf8.go, decodeRuneInStringInternal's implementations is the same with decodeRuneInternal.

WHY?

答案1

得分: 1

这两个函数避免了在转换[]byte(s)时进行内存分配,这种情况下字符串函数包装了[]byte函数,或者在string(p)转换中进行内存分配,这种情况下[]byte函数包装了字符串函数。

英文:

The two functions avoid the memory allocation in the conversion []byte(s) in the case where the string function wraps the []byte function or the memory allocation in the conversion string(p) in the case where the []byte function wraps the string function.

huangapple
  • 本文由 发表于 2016年1月16日 11:42:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/34823289.html
匿名

发表评论

匿名网友

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

确定