Skip to content

Added implementation of NSString init(contentsOfFile:usedEncoding:) #1059

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

Closed
wants to merge 1 commit into from

Conversation

andybest
Copy link

I have added an implementation for init(contentsOfFile:usedEncoding:)

After discussion on the mailing list, it was determined that the original implementation:

  1. Checks extended file attributes for "com.apple.TextEncoding", and uses the encoding there.
  2. If that doesn't exist, it checks the first few bytes of the data for a Byte Order Mark (BOM).
  3. If there is no BOM present, just try to decode as UTF8.

I also noticed that the init(contentsOf:usedEncoding:) initializer only looked for UTF16 BOMs, so I have updated the implementation for that too.

I have added some tests for the BOM detection, and updated the status document accordingly.

@andybest andybest force-pushed the NSStringInitUsedEncoding branch from cb7dfb1 to 632a837 Compare June 22, 2017 15:12
if String._conditionallyBridgeFromObjectiveC(cf._nsObject, result: &str) {
self.init(str!)
} else {
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileReadUnknownStringEncoding.rawValue, userInfo: [
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems more like a fatal error than an NSError here.

Copy link
Author

Choose a reason for hiding this comment

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

Which one, the bridging? I was going by other code that handled the same thing- I can convert to a fatal error if that's preferred.

@parkera
Copy link
Contributor

parkera commented Jun 22, 2017

@swift-ci please test

@parkera
Copy link
Contributor

parkera commented Jun 22, 2017

Looks pretty good, thanks for taking this one.

@pushkarnk
Copy link
Member

Foundation/NSString.swift:1396:24: error: use of unresolved identifier 'getxattr'
        let bufCount = getxattr(path, attrName, nil, 0, 0, 0)
                       ^~~~~~~~

@andybest
Copy link
Author

I forgot to add include GLibc to my commit. I'll add it and fix the merge conflicts

@andybest andybest force-pushed the NSStringInitUsedEncoding branch from 632a837 to c5463dc Compare June 26, 2017 09:14
Reworked implementation of NSString init(contentsOf:usedEncoding:) to use same BOM detection code
@andybest
Copy link
Author

andybest commented Jun 26, 2017

Ok, looks like the module map for Glibc doesn't include sys/xattr.h, so I've enabled the xattr check only for MacOS for now. I will add the include to the modulemap in the Swift project and do a PR over there to get it included, then I will make a separate PR here to re-enable this functionality under Linux.

I believe this PR should be ready to go here, in any case :)

Would it be possible to re-test this PR?

@ianpartridge
Copy link
Contributor

Hi @andybest did you open a PR against the Glibc modulemap? Just wondering where we're up to with this PR 😃

@alblue
Copy link
Contributor

alblue commented Jul 20, 2017

@swift-ci please test

@andybest
Copy link
Author

andybest commented Jul 22, 2017

@ianpartridge Apologies, I've been away and haven't had the chance- I will sort this out today :)

EDIT: I have now submitted the PR against the Glibc modulemap :)

@andybest
Copy link
Author

By the way, this PR can be merged as-is, since there is an '#if os(' block around the missing functionality which can removed later

@ianpartridge
Copy link
Contributor

@swift-ci please test

@alblue
Copy link
Contributor

alblue commented Jul 31, 2017

There were test failures on this run:

Test Case 'TestNSString.test_FromContentsOfFileUsedEncodingUTF32LEBOM' started at 2017-07-28 22:48:00.244
TestFoundation/TestNSString.swift:381: error: TestNSString.test_FromContentsOfFileUsedEncodingUTF32LEBOM : failed - URL for NSString-UTF32-LE-data.txt is nil
Test Case 'TestNSString.test_FromContentsOfFileUsedEncodingUTF32LEBOM' failed (0.0 seconds)
Test Case 'TestNSString.test_FromContentsOfFileUsedEncodingUTF32BEBOM' started at 2017-07-28 22:48:00.244
TestFoundation/TestNSString.swift:400: error: TestNSString.test_FromContentsOfFileUsedEncodingUTF32BEBOM : failed - URL for NSString-UTF32-BE-data.txt is nil

Did those files get added correctly?

@spevans
Copy link
Contributor

spevans commented Jul 31, 2017

Do the tests in the PR pass in Xcode? I noticed the tests for the UTF16 LE/BE were failing the other day and I was wondering if this fixes them as well?

@alblue
Copy link
Contributor

alblue commented Jul 31, 2017

This pull request seems to duplicate info in pull #928. Can you rebase and resubmit if necessary?

#928

@alblue
Copy link
Contributor

alblue commented Oct 5, 2017

This pull request has gone stale, closing. Please rebase and reopen if desired. See also #928

@alblue alblue closed this Oct 5, 2017
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.

6 participants