英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论