Skip to content

Commit 7852a63

Browse files
committed
- Make string escaping also handle Windows-style newlines
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@12132 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 3ce6b80 commit 7852a63

File tree

10 files changed

+1418
-1408
lines changed

10 files changed

+1418
-1408
lines changed

Compiler/Template/AbsynDumpTpl.mo

Lines changed: 1398 additions & 1398 deletions
Large diffs are not rendered by default.

Compiler/Util/System.mo

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,8 +791,9 @@ public function escapedString
791791
"Because list() requires escape-sequences to be in the AST, we need to be
792792
able to unescape them in some places of the code."
793793
input String unescapedString;
794+
input Boolean unescapeNewline;
794795
output String escapedString;
795-
external "C" escapedString=System_escapedString(unescapedString) annotation(Library = "omcruntime");
796+
external "C" escapedString=System_escapedString(unescapedString,unescapeNewline) annotation(Library = "omcruntime");
796797
end escapedString;
797798

798799
public function unescapedString

Compiler/Util/Util.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2043,7 +2043,7 @@ public function escapeModelicaStringToCString
20432043
output String cString;
20442044
algorithm
20452045
// C cannot handle newline in string constants
2046-
cString := System.stringReplace(System.escapedString(modelicaString), "\n", "\\n");
2046+
cString := System.escapedString(modelicaString,true);
20472047
end escapeModelicaStringToCString;
20482048

20492049
public function escapeModelicaStringToXmlString

Compiler/runtime/System_omc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,9 +579,9 @@ extern void* System_regex(const char* str, const char* re, int maxn, int extende
579579
return res;
580580
}
581581

582-
extern char* System_escapedString(char* str)
582+
extern char* System_escapedString(char* str, int nl)
583583
{
584-
char *res = SystemImpl__escapedString(str);
584+
char *res = SystemImpl__escapedString(str,nl);
585585
if (res == NULL) return str;
586586
return res;
587587
}

Compiler/runtime/System_rml.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1910,7 +1910,7 @@ RML_END_LABEL
19101910

19111911
RML_BEGIN_LABEL(System__escapedString)
19121912
{
1913-
char *str = SystemImpl__escapedString(RML_STRINGDATA(rmlA0));
1913+
char *str = SystemImpl__escapedString(RML_STRINGDATA(rmlA0),RML_UNTAGFIXNUM(rmlA1));
19141914
if (str == NULL) {
19151915
RML_TAILCALLK(rmlSC);
19161916
} else {

Compiler/runtime/systemimpl.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,7 @@ extern char* SystemImpl__unescapedString(const char* str)
11681168
return res;
11691169
}
11701170

1171-
extern int SystemImpl__escapedStringLength(const char* str)
1171+
extern int SystemImpl__escapedStringLength(const char* str, int nl)
11721172
{
11731173
int i=0;
11741174
while (*str) {
@@ -1179,20 +1179,25 @@ extern int SystemImpl__escapedStringLength(const char* str)
11791179
case '\b':
11801180
case '\f':
11811181
case '\v': i++;
1182+
case '\r': i++; if (nl && str[1] == '\n') str++;
1183+
case '\n': i++; if (nl && str[1] == '\r') str++;
11821184
default: i++;
11831185
}
11841186
str++;
11851187
}
11861188
return i;
11871189
}
11881190

1189-
extern char* SystemImpl__escapedString(const char* str)
1191+
/* "\b",_ => "\\b"
1192+
* "\n",true => "\\n"
1193+
*/
1194+
extern char* SystemImpl__escapedString(const char* str, int nl)
11901195
{
11911196
int len1,len2;
11921197
char *res;
11931198
int i=0;
11941199
len1 = strlen(str);
1195-
len2 = SystemImpl__escapedStringLength(str);
1200+
len2 = SystemImpl__escapedStringLength(str,nl);
11961201
if (len1 == len2) return NULL;
11971202
res = (char*) malloc(len2+1);
11981203
while (*str) {
@@ -1203,6 +1208,8 @@ extern char* SystemImpl__escapedString(const char* str)
12031208
case '\b': res[i++] = '\\'; res[i++] = 'b'; break;
12041209
case '\f': res[i++] = '\\'; res[i++] = 'f'; break;
12051210
case '\v': res[i++] = '\\'; res[i++] = 'v'; break;
1211+
case '\r': if (nl) {res[i++] = '\\'; res[i++] = 'n'; if (str[1] == '\n') str++;} else {res[i++] = *str;} break;
1212+
case '\n': if (nl) {res[i++] = '\\'; res[i++] = 'n'; if (str[1] == '\r') str++;} else {res[i++] = *str;} break;
12061213
default: res[i++] = *str;
12071214
}
12081215
str++;

Compiler/susan_codegen/SimCode/CodegenC.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ template globalDataVarInfoArray(String _name, list<SimVar> items, Integer offset
409409
case items then
410410
<<
411411
const struct VAR_INFO <%_name%>[<%listLength(items)%>] = {
412-
<%items |> var as SIMVAR(source=SOURCE(info=info as INFO(__))) => '{<%System.tmpTick()%>,"<%escapedString(crefStr(var.name))%>","<%Util.escapeModelicaStringToCString(var.comment)%>",{<%infoArgs(info)%>}}'; separator=",\n"%>
412+
<%items |> var as SIMVAR(source=SOURCE(info=info as INFO(__))) => '{<%System.tmpTick()%>,"<%escapedString(crefStr(var.name),true)%>","<%Util.escapeModelicaStringToCString(var.comment)%>",{<%infoArgs(info)%>}}'; separator=",\n"%>
413413
};
414414
<%items |> var as SIMVAR(source=SOURCE(info=info as INFO(__))) hasindex i0 => '#define <%cref(var.name)%>__varInfo <%_name%>[<%i0%>]'; separator="\n"%>
415415
>>

Compiler/susan_codegen/SimCode/ExpressionDumpTV.mo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,7 @@ end Expression;
746746
package System
747747
function escapedString
748748
input String unescapedString;
749+
input Boolean escapeNewline;
749750
output String escapedString;
750751
end escapedString;
751752
end System;

Compiler/susan_codegen/SimCode/ExpressionDumpTpl.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ match exp
1010
case ICONST(__) then integer
1111
case RCONST(__) then real
1212
case SCONST(__) then
13-
let str = escapedString(string)
13+
let str = escapedString(string,false)
1414
'<%stringDelimiter%><%str%><%stringDelimiter%>'
1515
case BCONST(__) then bool
1616
case ENUM_LITERAL(__) then AbsynDumpTpl.dumpPath(name)

Compiler/susan_codegen/SimCode/SimCodeTV.mo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ package System
702702

703703
function escapedString
704704
input String unescapedString;
705+
input Boolean unescapeNewline;
705706
output String escapedString;
706707
end escapedString;
707708

0 commit comments

Comments
 (0)