summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJürg Billeter <j@bitron.ch>2010-01-23 00:03:06 +0100
committerJürg Billeter <j@bitron.ch>2010-03-14 20:25:35 +0100
commit20666769c446ece07ef1063188d3ac5879c9f84c (patch)
tree2d54f6908b43395f8122992868dd1c2069eae2c6
parent20db7f59c9ae1d9ea157adacb91e73666e53f69b (diff)
downloadvala-20666769c446ece07ef1063188d3ac5879c9f84c.tar.gz
Split large string literals into multiple lines
Based on patch by Marc-André Lureau, fixes bug 606838.
-rw-r--r--ccode/valaccodeconstant.vala63
-rw-r--r--codegen/valaccodebasemodule.vala2
2 files changed, 61 insertions, 4 deletions
diff --git a/ccode/valaccodeconstant.vala b/ccode/valaccodeconstant.vala
index ebf36e9c6..0ac49ec3d 100644
--- a/ccode/valaccodeconstant.vala
+++ b/ccode/valaccodeconstant.vala
@@ -1,6 +1,6 @@
/* valaccodeconstant.vala
*
- * Copyright (C) 2006 Jürg Billeter
+ * Copyright (C) 2006-2010 Jürg Billeter
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -30,11 +30,68 @@ public class Vala.CCodeConstant : CCodeExpression {
* The name of this constant.
*/
public string name { get; set; }
-
+
public CCodeConstant (string _name) {
name = _name;
}
-
+
+ const int LINE_LENGTH = 70;
+
+ public CCodeConstant.string (string _name) {
+ assert (_name[0] == '\"');
+
+ if (_name.length <= LINE_LENGTH) {
+ name = _name;
+ return;
+ }
+
+ var builder = new StringBuilder ("\"");
+
+ char* p = _name;
+ char* end = p + _name.size ();
+
+ // remove quotes
+ p++;
+ end--;
+
+ int col = 0;
+ while (p < end) {
+ if (col >= LINE_LENGTH) {
+ builder.append ("\" \\\n\"");
+ col = 0;
+ }
+ if (*p == '\\') {
+ char* begin_of_char = p;
+
+ builder.append_c (p[0]);
+ builder.append_c (p[1]);
+ p += 2;
+ switch (p[-1]) {
+ case 'x':
+ // hexadecimal character
+ while (p < end && p->isxdigit ()) {
+ builder.append_c (*p);
+ p++;
+ }
+ break;
+ case 'n':
+ // break line at \n
+ col = LINE_LENGTH;
+ break;
+ }
+ col += (int) (p - begin_of_char);
+ } else {
+ builder.append_unichar (((string) p).get_char ());
+ p += ((char*) ((string) p).next_char () - p);
+ col++;
+ }
+ }
+
+ builder.append_c ('"');
+
+ this.name = builder.str;
+ }
+
public override void write (CCodeWriter writer) {
writer.write_string (name);
}
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 428fdadaa..fe9d70e0c 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -3480,7 +3480,7 @@ internal class Vala.CCodeBaseModule : CCodeModule {
}
public override void visit_string_literal (StringLiteral expr) {
- expr.ccodenode = new CCodeConstant (expr.value);
+ expr.ccodenode = new CCodeConstant.string (expr.value);
}
public override void visit_null_literal (NullLiteral expr) {