Repository: Konstantin8105/c4go
Pull request #295 (2019-02-28 11:35:49)

bytes literal : add initialization

codecov[bot]: # [Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/295?src=pr&el=h1) Report
> :exclamation: No coverage uploaded for pull request base (`master@fed3c68`). [Click here to learn what that means](https://docs.codecov.io/docs/error-reference#section-missing-base-commit).
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/Konstantin8105/c4go/pull/295/graphs/tree.svg?width=650&token=esH2rHXlZ2&height=150&src=pr)](https://codecov.io/gh/Konstantin8105/c4go/pull/295?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##             master     #295   +/-   ##
=========================================
  Coverage          ?   86.82%           
=========================================
  Files             ?      184           
  Lines             ?    10011           
  Branches          ?        0           
=========================================
  Hits              ?     8692           
  Misses            ?      911           
  Partials          ?      408
```


| [Impacted Files](https://codecov.io/gh/Konstantin8105/c4go/pull/295?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [transpiler/variables.go](https://codecov.io/gh/Konstantin8105/c4go/pull/295/diff?src=pr&el=tree#diff-dHJhbnNwaWxlci92YXJpYWJsZXMuZ28=) | `84.35% <100%> (ø)` | |
| [transpiler/literals.go](https://codecov.io/gh/Konstantin8105/c4go/pull/295/diff?src=pr&el=tree#diff-dHJhbnNwaWxlci9saXRlcmFscy5nbw==) | `81.81% <100%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/295?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/295?src=pr&el=footer). Last update [fed3c68...8a21fc5](https://codecov.io/gh/Konstantin8105/c4go/pull/295?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/transpiler/literals.go b/transpiler/literals.go
index 60422c1..b1c616e 100644
--- a/transpiler/literals.go
+++ b/transpiler/literals.go
@@ -100,8 +100,8 @@ func transpileStringLiteral(p *program.Program, n *ast.StringLiteral, arrayToArr
 	// 	return b
 	// }()}
 	expr = goast.NewIdent(fmt.Sprintf(
-		"func() (b [%v]byte) { copy(b[:],\"%s\" );return }()",
-		s, n.Value))
+		"func() (b [%v]byte) {copy(b[:], %s);return }()",
+		s, strconv.Quote(n.Value)))
 	exprType = n.Type
 	return
 }
diff --git a/transpiler/variables.go b/transpiler/variables.go
index f016291..e7877bf 100644
--- a/transpiler/variables.go
+++ b/transpiler/variables.go
@@ -219,6 +219,9 @@ func transpileInitListExpr(e *ast.InitListExpr, p *program.Program) (
 		var err error
 		if sl, ok := node.(*ast.StringLiteral); ok {
 			expr, _, err = transpileStringLiteral(p, sl, true)
+			if _, ok := p.Structs[e.Type1]; !ok {
+				expr, _, err = transpileStringLiteral(p, sl, false)
+			}
 		} else {
 			expr, _, _, _, err = transpileToExpr(node, p, true)
 		}


Test patch:
diff --git a/tests/cast.c b/tests/cast.c
index 684d232..590217f 100644
--- a/tests/cast.c
+++ b/tests/cast.c
@@ -139,9 +139,18 @@ void test_unsafe_pnt()
     // }
 }
 
+void test_init()
+{
+	char ch[4][10] = {"\"", "\n", "\\", "a"};
+	is_streq(ch[0], "\"");
+	is_streq(ch[1], "\n");
+	is_streq(ch[2], "\\");
+	is_streq(ch[3], "a");
+}
+
 int main()
 {
-    plan(38);
+    plan(42);
 
     START_TEST(unsafe_pnt);
     START_TEST(bool_to_int);
@@ -149,6 +158,7 @@ int main()
     START_TEST(castbool);
     START_TEST(vertex);
     START_TEST(strCh);
+    START_TEST(init);
 
     {
         typedef unsigned int u32;