From 19497b2d27906a44bfeb39a804f47d24e27f1fef Mon Sep 17 00:00:00 2001 From: Elliot Chance Date: Sun, 23 Jul 2017 21:00:35 +1000 Subject: [PATCH] CStringToString --- darwin/assert.go | 6 +++--- linux/assert.go | 6 +++--- noarch/stdio.go | 24 ++++++++++++------------ noarch/string.go | 2 +- noarch/util.go | 4 ++-- noarch/util_test.go | 4 ++-- transpiler/call.go | 4 ++-- 7 files changed, 25 insertions(+), 25 deletions(-) diff --git a/darwin/assert.go b/darwin/assert.go index 92b5e527c..b4ad8b85a 100644 --- a/darwin/assert.go +++ b/darwin/assert.go @@ -21,9 +21,9 @@ func AssertRtn( fmt.Fprintf( os.Stderr, "Assertion failed: (%s), function %s, file %s, line %d.\n", - noarch.NullTerminatedByteSlice(expression), - noarch.NullTerminatedByteSlice(functionName), - noarch.NullTerminatedByteSlice(filePath), + noarch.CStringToString(expression), + noarch.CStringToString(functionName), + noarch.CStringToString(filePath), lineNumber, ) os.Exit(134) diff --git a/linux/assert.go b/linux/assert.go index cfce25b8b..fef521790 100644 --- a/linux/assert.go +++ b/linux/assert.go @@ -16,10 +16,10 @@ func AssertFail( fmt.Fprintf( os.Stderr, "a.out: %s:%d: %s: Assertion `%s' failed.\n", - noarch.NullTerminatedByteSlice(filePath), + noarch.CStringToString(filePath), lineNumber, - noarch.NullTerminatedByteSlice(functionName), - noarch.NullTerminatedByteSlice(expression), + noarch.CStringToString(functionName), + noarch.CStringToString(expression), ) os.Exit(134) diff --git a/noarch/stdio.go b/noarch/stdio.go index 6221502ad..df0e8e167 100644 --- a/noarch/stdio.go +++ b/noarch/stdio.go @@ -54,11 +54,11 @@ func Fopen(filePath, mode []byte) *File { var file *os.File var err error - sFilePath := NullTerminatedByteSlice(filePath) + sFilePath := CStringToString(filePath) // TODO: Only some modes are supported by fopen() // https://github.com/elliotchance/c2go/issues/89 - switch NullTerminatedByteSlice(mode) { + switch CStringToString(mode) { case "r": file, err = os.Open(sFilePath) case "r+": @@ -107,7 +107,7 @@ func Fclose(f *File) int { // // Proper file access shall be available. func Remove(filePath []byte) int { - if os.Remove(NullTerminatedByteSlice(filePath)) != nil { + if os.Remove(CStringToString(filePath)) != nil { return -1 } @@ -130,8 +130,8 @@ func Remove(filePath []byte) int { // // Proper file access shall be available. func Rename(oldName, newName []byte) int { - from := NullTerminatedByteSlice(oldName) - to := NullTerminatedByteSlice(newName) + from := CStringToString(oldName) + to := CStringToString(newName) if os.Rename(from, to) != nil { return -1 @@ -359,13 +359,13 @@ func Fprintf(f *File, format []byte, args ...interface{}) int { typeOfByteSlice := reflect.TypeOf([]byte(nil)) for _, arg := range args { if reflect.TypeOf(arg) == typeOfByteSlice { - realArgs = append(realArgs, NullTerminatedByteSlice(arg.([]byte))) + realArgs = append(realArgs, CStringToString(arg.([]byte))) } else { realArgs = append(realArgs, arg) } } - n, err := fmt.Fprintf(f.OsFile, NullTerminatedByteSlice(format), realArgs...) + n, err := fmt.Fprintf(f.OsFile, CStringToString(format), realArgs...) if err != nil { return -1 } @@ -384,7 +384,7 @@ func Fprintf(f *File, format []byte, args ...interface{}) int { func Fscanf(f *File, format []byte, args ...interface{}) int { realArgs := prepareArgsForScanf(args) - n, err := fmt.Fscanf(f.OsFile, NullTerminatedByteSlice(format), realArgs...) + n, err := fmt.Fscanf(f.OsFile, CStringToString(format), realArgs...) if err != nil { panic(err) return -1 @@ -621,13 +621,13 @@ func Printf(format []byte, args ...interface{}) int { typeOfByteSlice := reflect.TypeOf([]byte(nil)) for _, arg := range args { if reflect.TypeOf(arg) == typeOfByteSlice { - realArgs = append(realArgs, NullTerminatedByteSlice(arg.([]byte))) + realArgs = append(realArgs, CStringToString(arg.([]byte))) } else { realArgs = append(realArgs, arg) } } - n, _ := fmt.Printf(NullTerminatedByteSlice(format), realArgs...) + n, _ := fmt.Printf(CStringToString(format), realArgs...) return n } @@ -645,7 +645,7 @@ func Printf(format []byte, args ...interface{}) int { // destination, but it also appends a newline character at the end automatically // (which fputs does not). func Puts(str []byte) int { - n, _ := fmt.Println(NullTerminatedByteSlice(str)) + n, _ := fmt.Println(CStringToString(str)) return n } @@ -660,7 +660,7 @@ func Puts(str []byte) int { // string. func Scanf(format []byte, args ...interface{}) int { realArgs := prepareArgsForScanf(args) - n, _ := fmt.Scanf(NullTerminatedByteSlice(format), realArgs...) + n, _ := fmt.Scanf(CStringToString(format), realArgs...) finalizeArgsForScanf(realArgs, args) return n diff --git a/noarch/string.go b/noarch/string.go index 89cd6db84..4a846b118 100644 --- a/noarch/string.go +++ b/noarch/string.go @@ -10,5 +10,5 @@ func Strlen(a []byte) int { // TODO: The transpiler should have a syntax that means this proxy function // does not need to exist. - return len(NullTerminatedByteSlice(a)) + return len(CStringToString(a)) } diff --git a/noarch/util.go b/noarch/util.go index a8cdf868e..ab63dd610 100644 --- a/noarch/util.go +++ b/noarch/util.go @@ -4,9 +4,9 @@ import ( "reflect" ) -// NullTerminatedByteSlice returns a string that contains all the bytes in the +// CStringToString returns a string that contains all the bytes in the // provided C string up until the first NULL character. -func NullTerminatedByteSlice(s []byte) string { +func CStringToString(s []byte) string { if s == nil { return "" } diff --git a/noarch/util_test.go b/noarch/util_test.go index 39423979d..6aed139ca 100644 --- a/noarch/util_test.go +++ b/noarch/util_test.go @@ -22,8 +22,8 @@ func TestNullTerminatedByteSlice(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := NullTerminatedByteSlice(tt.args.s); !reflect.DeepEqual(got, tt.want) { - t.Errorf("NullTerminatedByteSlice() = %v, want %v", got, tt.want) + if got := CStringToString(tt.args.s); !reflect.DeepEqual(got, tt.want) { + t.Errorf("CStringToString() = %v, want %v", got, tt.want) } }) } diff --git a/transpiler/call.go b/transpiler/call.go index 4b2052f12..c8583462b 100644 --- a/transpiler/call.go +++ b/transpiler/call.go @@ -122,7 +122,7 @@ func transpileCallExpr(n *ast.CallExpr, p *program.Program) ( if arraySize != -1 { p.AddImport("github.com/elliotchance/c2go/noarch") e = util.NewCallExpr( - "noarch.NullTerminatedByteSlice", + "noarch.CStringToString", &goast.SliceExpr{X: e}, ) } @@ -134,7 +134,7 @@ func transpileCallExpr(n *ast.CallExpr, p *program.Program) ( if i > len(functionDef.ArgumentTypes)-1 && (eType == "char *" || eType == "char*") { p.AddImport("github.com/elliotchance/c2go/noarch") - e = util.NewCallExpr("noarch.NullTerminatedByteSlice", e) + e = util.NewCallExpr("noarch.CStringToString", e) } }