From: Jürg Billeter Date: Fri, 22 Jan 2010 23:03:06 +0000 (+0100) Subject: Split large string literals into multiple lines X-Git-Tag: 0.8.0~173 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=20666769c446ece07ef1063188d3ac5879c9f84c;p=thirdparty%2Fvala.git Split large string literals into multiple lines Based on patch by Marc-André Lureau, fixes bug 606838. --- 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) {