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

Add Shift-JIS encoding support #29

Merged
merged 1 commit into from
Nov 22, 2020
Merged

Add Shift-JIS encoding support #29

merged 1 commit into from
Nov 22, 2020

Conversation

rkyymmt
Copy link

@rkyymmt rkyymmt commented Nov 17, 2020

Description

Add Shift-JIS encoding support.
(Shift-JIS used in Japan.)

Copy link
Owner

@dehesa dehesa left a comment

Choose a reason for hiding this comment

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

Thank you for the PR. The changes seems good 👍

Once you have perform the comments, I am happy to merge. Would be too much to ask to add a small test to verify the usage? 😅

default: throw Error._unsupported(encoding: encoding)
}
}
}

extension Data {
Copy link
Owner

Choose a reason for hiding this comment

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

Please, make the extension fileprivate.

default: throw Error._unsupported(encoding: encoding)
}
}
}

extension Data {
var encodedHexadecimals: [UInt8]? {
Copy link
Owner

Choose a reason for hiding this comment

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

private or fileprivate variables and functions are underscored in this library.

var encodedHexadecimals: [UInt8]? {
let responseValues = self.withUnsafeBytes({ (pointer: UnsafeRawBufferPointer) -> [UInt8] in
let unsafeBufferPointer = pointer.bindMemory(to: UInt8.self)
let unsafePointer = unsafeBufferPointer.baseAddress!
Copy link
Owner

Choose a reason for hiding this comment

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

Since this code will be repeated very often, I believe it is worth to change the force cast into a unsafelyUnwrapped.

@dehesa
Copy link
Owner

dehesa commented Nov 22, 2020

Sigh, I guess it would. I will make the changes myself.

@dehesa dehesa merged commit bef29ba into dehesa:develop Nov 22, 2020
@dehesa
Copy link
Owner

dehesa commented Nov 22, 2020

Alright, @rkyymmt and @rikiya-yamamoto. I have taken a deeper look at your code and saw that you had an unnecessary array allocation that was being executed for every scalar. I removed it and access the underlying byte representation directly.

Performance should now be better, but still it is far from ideal since we are performing a round trip from Unicode Scalar -> String -> to Data. I am not very knowledgable on ShiftJIS, but I am sure there must be a way to transform Unicode Scalar to UInt8 directly.

In any case, the code should now work and be faster than what you sent. Please test it out with your data.

Regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants