Changeset 363

Show
Ignore:
Timestamp:
06/23/08 19:39:03 (5 months ago)
Author:
FeepingCreature
Message:
  • Added string literal expansion compile time function
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tools/tools/base.d

    r361 r363  
    11341134 
    11351135T Cast(S, T)(S orig) { return cast(T) orig; } 
     1136 
     1137string litstring_expand(string str) { 
     1138  string result; 
     1139  int depth = 0; 
     1140  int i; 
     1141  for (i = 0; i < str.length; ++i) { 
     1142    if (i < str.length - 1) { 
     1143      if (str[i] == 'q' && str[i+1] == '{') { 
     1144          int count; 
     1145          for (int k = 0; k < depth; ++k) count = count * 2 + 1; 
     1146          for (int k = 0; k < count; ++k) result ~= "\\"; 
     1147        result ~= "\""; 
     1148        depth ++; 
     1149        ++i; 
     1150      } else if (str[i .. i+2] == "q}") { 
     1151        depth --; 
     1152          int count; 
     1153          for (int k = 0; k < depth; ++k) count = count * 2 + 1; 
     1154          for (int k = 0; k < count; ++k) result ~= "\\"; 
     1155        result ~= "\""; 
     1156        ++i; 
     1157      } else result ~= str[i]; 
     1158    } else result ~= str[i]; 
     1159  } 
     1160  return result; 
     1161} 
     1162 
     1163static assert(litstring_expand("q{ foo; q{ bar; q} q}") == "\" foo; \\\" bar; \\\" \""); 
     1164static assert(litstring_expand("q{ foo; q{ bar; q} q} ") == "\" foo; \\\" bar; \\\" \" ");