Skip to content

Commit

Permalink
doc(Python): Document TODO to refactor string conversion (#587)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmcdonald3 authored Sep 17, 2024
1 parent dcc45dc commit 9dd74be
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ public String stringShape(StringShape shape) {
}

// Convert native Python string to Dafny Seq of UTF-16 characters
// TODO: This is a long conversion that is used often in generated code, since this is written for *all* strings.
// This should be refactored into the conversionwriter package.
return "Seq(''.join([chr(int.from_bytes(pair, 'big')) for pair in zip(*[iter(%1$s.encode('utf-16-be'))]*2)]))".formatted(
dataSource
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ public String stringShape(StringShape shape) {
return enumShape(EnumShape.fromStringShape(shape).get());
}
// Convert Dafny Seq of UTF-16 characters to native Python string
// TODO: This is a long conversion that is used often in generated code, since this is written for *all* strings.
// This should be refactored into the conversionwriter package.
return "b''.join(ord(c).to_bytes(2, 'big') for c in %1$s).decode('utf-16-be')".formatted(
dataSource
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ public String stringShape(StringShape shape) {
return "bytes(%1$s.Elements).decode('utf-8')".formatted(dataSource);
}
// Convert Dafny Seq of UTF-16 characters to native Python string
// TODO: This is a long conversion that is used often in generated code, since this is written for *all* strings.
// This should be refactored into the conversionwriter package.
return "b''.join(ord(c).to_bytes(2, 'big') for c in %1$s).decode('utf-16-be')".formatted(
dataSource
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ public String stringShape(StringShape shape) {
return "Seq(%1$s.encode('utf-8'))".formatted(dataSource);
}
// Convert native Python string to Dafny Seq of UTF-16 characters
// TODO: This is a long conversion that is used often in generated code, since this is written for *all* strings.
// This should be refactored into the conversionwriter package.
return "Seq(''.join([chr(int.from_bytes(pair, 'big')) for pair in zip(*[iter(%1$s.encode('utf-16-be'))]*2)]))".formatted(
dataSource
);
Expand Down

0 comments on commit 9dd74be

Please sign in to comment.