Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PreSmalltalks] Implement LH "Unsafe Symbol" encoding #385

Merged
merged 1 commit into from
Nov 12, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ BaselineOfMachineArithmetic >> baseline: spec [
package:#'PreSmalltalks-Applicative';
package:#'PreSmalltalks-Substitutions';
package:#'PreSmalltalks-ZEncoding';
package:#'PreSmalltalks-FxEncoding';
package: #'PreSmalltalks' with:
[spec requires: 'PreSmalltalks-Pharo';
requires: 'PreSmalltalks-Applicative';
requires: 'PreSmalltalks-Substitutions';
requires: 'PreSmalltalks-ZEncoding'
requires: 'PreSmalltalks-ZEncoding';
requires: 'PreSmalltalks-FxEncoding'
];
package: #'PreSmalltalks-Parser' with:
[spec requires: 'PreSmalltalks';
Expand Down
18 changes: 18 additions & 0 deletions src/PreSmalltalks-FxEncoding/Character.extension.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Extension { #name : #Character }

{ #category : #'*PreSmalltalks-FxEncoding' }
Character >> isUnsafeChar [
"
isUnsafeChar :: Char -> Bool
Cf. Names.hs
"
^(Character okSymChars includes: self) not
]

{ #category : #'*PreSmalltalks-FxEncoding' }
Character class >> okSymChars [
^($a to: $z),
($A to: $Z),
($0 to: $9),
{$_}
]
43 changes: 43 additions & 0 deletions src/PreSmalltalks-FxEncoding/String.extension.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Extension { #name : #String }

{ #category : #'*PreSmalltalks-FxEncoding' }
String >> decodeUnsafe [
| in out |
in := ReadStream on: self.
out := WriteStream on: String new.
[ in atEnd ] whileFalse: [ | c |
c := in next.
c = $ß
ifTrue: [ | codePoint |
codePoint := Integer readFrom: (in upTo: $ß).
out nextPut: (Character codePoint: codePoint)
] ifFalse: [
out nextPut: c
]
].
^out contents
"
'vß35ß3ß35ßß35ß1' decodeUnsafe → 'v#3##1'
"
]

{ #category : #'*PreSmalltalks-FxEncoding' }
String >> encodeUnsafe [
"
encodeUnsafe :: T.Text -> T.Text
Cf. Names.hs
In Smalltalk, we want to satisfy stricter requirements:
we want our symbols to be valid Z3 symbols (even after passing
through FFI) AND be valid Smalltalk names.
Therefore '$' is not a safe character; but 'ß' is.
"
| c cs head tail |
self isEmpty ifTrue: [ ^ self ].
c := self first.
cs := self allButFirst.
head := c isUnsafeChar
ifTrue: [ 'ß' , c codePoint printString , 'ß' ]
ifFalse: [ String with: c ].
tail := cs encodeUnsafe.
^ head , tail
]
1 change: 1 addition & 0 deletions src/PreSmalltalks-FxEncoding/package.st
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Package { #name : #'PreSmalltalks-FxEncoding' }
Loading