Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions mathics/builtin/optiondoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ class Bottom(Builtin):
"""


class CharacterEncodingOption(Builtin):
"""
<dl>
<dt>'CharacterEncoding'
<dd>is an option for input and output functions which specifies what raw character encoding should be used.
</dl>

>> ToString["\[CirclePlus]", CharacterEncoding -> "UTF8"]
= ...
"""


class Filling(Builtin):
"""
<dl>
Expand Down
13 changes: 11 additions & 2 deletions mathics/builtin/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1661,14 +1661,23 @@ class ToString(Builtin):
"TotalWidth": "Infinity",
}

messages = {"encoding": "Encoding not implemented for ToString."}


def apply_default(self, value, evaluation, options):
"ToString[value_, OptionsPattern[ToString]]"
return self.apply_form(value, Symbol("System`OutputForm"), evaluation, options)

def apply_form(self, value, form, evaluation, options):
"ToString[value_, form_, OptionsPattern[ToString]]"
encoding = options["System`CharacterEncoding"]
text = value.format(evaluation, form.get_name(), encoding=encoding)
if form.has_form("Rule", 2) and form.leaves[0] == Symbol("System`CharacterEncoding"):
return evaluation.message("ToString", "encoding")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think before this message, an if clause checking that encoding is not available is missing. Otherwise, the next line is never executed.

encoding = form.leaves[1]
# form_name='System`OutputForm'
else:
encoding = options["System`CharacterEncoding"]
form_name = form.get_name()
text = value.format(evaluation, form_name, encoding=encoding)
text = text.boxes_to_text(evaluation=evaluation)
return String(text)

Expand Down