Implement convert string <- []byte
Этот коммит содержится в:
родитель
eed25c78df
коммит
fdc56d5940
2 изменённых файлов: 23 добавлений и 3 удалений
11
compiler.go
11
compiler.go
|
@ -2066,8 +2066,19 @@ func (c *Compiler) parseConvert(typeFrom, typeTo types.Type, value llvm.Value) (
|
||||||
sizeFrom := c.targetData.TypeAllocSize(llvmTypeFrom)
|
sizeFrom := c.targetData.TypeAllocSize(llvmTypeFrom)
|
||||||
|
|
||||||
if typeTo.Kind() == types.String {
|
if typeTo.Kind() == types.String {
|
||||||
|
switch typeFrom := typeFrom.Underlying().(type) {
|
||||||
|
case *types.Slice:
|
||||||
|
switch typeFrom.Elem().(*types.Basic).Kind() {
|
||||||
|
case types.Byte:
|
||||||
|
fn := c.mod.NamedFunction("runtime.stringFromBytes")
|
||||||
|
return c.builder.CreateCall(fn, []llvm.Value{value}, ""), nil
|
||||||
|
default:
|
||||||
return llvm.Value{}, errors.New("todo: convert to string: " + typeFrom.String())
|
return llvm.Value{}, errors.New("todo: convert to string: " + typeFrom.String())
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
return llvm.Value{}, errors.New("todo: convert to string: " + typeFrom.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
typeFrom := typeFrom.Underlying().(*types.Basic)
|
typeFrom := typeFrom.Underlying().(*types.Basic)
|
||||||
sizeTo := c.targetData.TypeAllocSize(llvmTypeTo)
|
sizeTo := c.targetData.TypeAllocSize(llvmTypeTo)
|
||||||
|
|
|
@ -9,7 +9,7 @@ import (
|
||||||
// The underlying struct for the Go string type.
|
// The underlying struct for the Go string type.
|
||||||
type _string struct {
|
type _string struct {
|
||||||
length lenType
|
length lenType
|
||||||
ptr *uint8
|
ptr *byte
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return true iff the strings match.
|
// Return true iff the strings match.
|
||||||
|
@ -36,6 +36,15 @@ func stringConcat(x, y _string) _string {
|
||||||
buf := alloc(length)
|
buf := alloc(length)
|
||||||
memcpy(buf, unsafe.Pointer(x.ptr), uintptr(x.length))
|
memcpy(buf, unsafe.Pointer(x.ptr), uintptr(x.length))
|
||||||
memcpy(unsafe.Pointer(uintptr(buf)+uintptr(x.length)), unsafe.Pointer(y.ptr), uintptr(y.length))
|
memcpy(unsafe.Pointer(uintptr(buf)+uintptr(x.length)), unsafe.Pointer(y.ptr), uintptr(y.length))
|
||||||
return _string{lenType(length), (*uint8)(buf)}
|
return _string{lenType(length), (*byte)(buf)}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create a string from a []byte slice.
|
||||||
|
func stringFromBytes(x []byte) _string {
|
||||||
|
buf := alloc(uintptr(len(x)))
|
||||||
|
for i, c := range x {
|
||||||
|
*(*byte)(unsafe.Pointer(uintptr(buf) + uintptr(i))) = c
|
||||||
|
}
|
||||||
|
return _string{lenType(len(x)), (*byte)(buf)}
|
||||||
|
}
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче