Skip to content

Escape sequences in string parsing #1622

@mmatera

Description

@mmatera

Description

Looking at #1619, I found a slight difference on how WMA and Mathics parse strings with escaped characters.

How to Reproduce

In CLI:

In[1]:="\\\` " //InputForm

Output Given

In Mathics3

Out[1]:="\\\\` " //InputForm

Expected behavior

In WMA,

Out[1]:="\\\` " //InputForm

The issue manifests when using StringForm, because we cannot put a backslash before a placeholder:

In[1]:=StringForm["C:\\`1`", "root"]

In WMA, we get

Out[1]:= C:\root

but in Mathics we get an error:

StringForm::sfq: Unmatched backquote in C:\`1`.
Out[1]= C:\`1`

Workarounds

The workaroung is to put the backslash in a parameter:

In[2]:=StringForm["C:`2``1`", "root", "\\"]
Out[2]= C:\root

In other contexts, it is enough to avoid escape the backquote character.

In[1]:= "\\`"

Priority

Low

Additional information

This is how WMA and Mathics process similar cases:

WMA

In[1]:=  cases = {"\\", "\ ", "\\ ", "\\\ ",                                    
  "`", "\` ", "\\` ", "\\\` "}                                                  

Out[1]= {\,  , \ , \ , `, \` , \` , \\` }

In[2]:= Table[{case, case // InputForm, StringLength[case],                     
   StringTake[case, {1}] }, {case, cases}] // TableForm                         

Out[2]//TableForm= \      "\\"      1   \

                          " "       1    

                   \      "\\ "     2   \

                   \      "\\ "     2   \

                   `      "`"       1   `

                   \`     "\` "     2   \`

                   \`     "\\` "    3   \

                   \\`    "\\\` "   3   \

Mathics

In[1]:=  cases = {"\\", "\ ", "\\ ", "\\\ ",
          "`", "\` ", "\\` ", "\\\` "}
Out[1]= {\,  , \ , \ , `, \` , \` , \\` }

In[2]:= Table[{case, case // InputForm, StringLength[case], 
           StringTake[case, {1}] }, {case, cases}] // TableForm
Out[2]//TableForm= \      "\\"       1   \
                   
                          " "        1    
                   
                   \      "\\ "      2   \
                   
                   \      "\\ "      2   \
                   
                   `      "`"        1   `
                   
                   \`     "\\` "     3   \
                   
                   \`     "\\` "     3   \
                   
                   \\`    "\\\\` "   4   \

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions