| | 1136 | |
|---|
| | 1137 | string 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 | |
|---|
| | 1163 | static assert(litstring_expand("q{ foo; q{ bar; q} q}") == "\" foo; \\\" bar; \\\" \""); |
|---|
| | 1164 | static assert(litstring_expand("q{ foo; q{ bar; q} q} ") == "\" foo; \\\" bar; \\\" \" "); |
|---|