Skip to content

Commit

Permalink
[translation] Switch to pyj8::WriteString()
Browse files Browse the repository at this point in the history
More spec-cpp tests now pass, e.g. in spec/ysh-json.
  • Loading branch information
Andy C committed Jan 12, 2024
1 parent 4791caa commit 4cb78d6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 21 deletions.
4 changes: 0 additions & 4 deletions cpp/data_lang.cc
Expand Up @@ -36,7 +36,6 @@ void WriteBString(BigStr* s, mylib::BufWriter* buf, int capacity) {
// Set up pointers after writing opening quote
uint8_t* out = buf->LengthPointer(); // mutated
uint8_t* out_end = buf->CapacityPointer();
uint8_t** p_out = &out;

while (true) {
J8EncodeChunk(&in, in_end, &out, out_end, true); // Fill as much as we can
Expand All @@ -54,7 +53,6 @@ void WriteBString(BigStr* s, mylib::BufWriter* buf, int capacity) {
// Recompute pointers
out = buf->LengthPointer();
out_end = buf->CapacityPointer();
p_out = &out;
}

buf->WriteConst("'");
Expand All @@ -81,7 +79,6 @@ void WriteString(BigStr* s, int options, mylib::BufWriter* buf) {
// Set up pointers after writing opening quote
uint8_t* out = buf->LengthPointer(); // mutated
uint8_t* out_end = buf->CapacityPointer();
uint8_t** p_out = &out;

while (true) {
// Fill in as much as we can
Expand Down Expand Up @@ -111,7 +108,6 @@ void WriteString(BigStr* s, int options, mylib::BufWriter* buf) {
// Recompute pointers
out = buf->LengthPointer(); // mutated
out_end = buf->CapacityPointer();
p_out = &out;
// printf("[1] out %p out_end %p\n", out, out_end);
}

Expand Down
22 changes: 5 additions & 17 deletions data_lang/j8.py
Expand Up @@ -223,17 +223,6 @@ def _GetIndent(self, num_spaces):
self.spaces[num_spaces] = ' ' * num_spaces
return self.spaces[num_spaces]

def _StringToBuf(self, s):
# type: (str) -> None

if mylib.PYTHON:
# TODO: port this to C++
pyj8.WriteString(s, self.options, self.buf)
else:
self.buf.write('"')
qsn.EncodeRunes(s, qsn.BIT8_UTF8, self.buf)
self.buf.write('"')

def Print(self, val, level=0):
# type: (value_t, int) -> None

Expand Down Expand Up @@ -278,8 +267,7 @@ def Print(self, val, level=0):
elif case(value_e.Str):
val = cast(value.Str, UP_val)

# TODO: pyj8.WriteString(val.s, self.buf)
self._StringToBuf(val.s)
pyj8.WriteString(val.s, self.options, self.buf)

elif case(value_e.List):
val = cast(value.List, UP_val)
Expand Down Expand Up @@ -339,7 +327,7 @@ def Print(self, val, level=0):

self.buf.write(item_indent)

self._StringToBuf(k)
pyj8.WriteString(k, self.options, self.buf)

self.buf.write(':')
self.buf.write(maybe_space)
Expand Down Expand Up @@ -373,7 +361,7 @@ def Print(self, val, level=0):
if s is None:
self.buf.write('null')
else:
self._StringToBuf(s)
pyj8.WriteString(s, self.options, self.buf)

self.buf.write(maybe_newline)

Expand All @@ -393,12 +381,12 @@ def Print(self, val, level=0):

self.buf.write(item_indent)

self._StringToBuf(k2)
pyj8.WriteString(k2, self.options, self.buf)

self.buf.write(':')
self.buf.write(maybe_space)

self._StringToBuf(v2)
pyj8.WriteString(v2, self.options, self.buf)

i += 1

Expand Down

0 comments on commit 4cb78d6

Please sign in to comment.