]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Split large string literals into multiple lines
authorJürg Billeter <j@bitron.ch>
Fri, 22 Jan 2010 23:03:06 +0000 (00:03 +0100)
committerJürg Billeter <j@bitron.ch>
Sun, 14 Mar 2010 19:25:35 +0000 (20:25 +0100)
Based on patch by Marc-André Lureau, fixes bug 606838.

ccode/valaccodeconstant.vala
codegen/valaccodebasemodule.vala

index ebf36e9c691cf2ce607af34be1cf0560701d93d4..0ac49ec3dcf6fbec2e0d3e5f32d035d782dcffe6 100644 (file)
@@ -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);
        }
index 428fdadaad7eba224a69801fc2c6c479e180ba95..fe9d70e0c21c11dd677b147b39c37ce77e7ae0c2 100644 (file)
@@ -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) {