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

[SR-5829] Different output in Foundation Linux versus Foundation OSx #4072

Closed
swift-ci opened this issue Sep 4, 2017 · 3 comments
Closed
Assignees

Comments

@swift-ci
Copy link
Contributor

swift-ci commented Sep 4, 2017

Previous ID SR-5829
Radar None
Original Reporter lf-araujo (JIRA User)
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment

Linux (Ubuntu)

OSx

Swift 3

Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee @spevans
Priority Medium

md5: c772f932a7dfd065482ae48ac08a6218

Issue Description:

While working in a small routine for finding a common directory path, the same function generated distinct results when run in OSx (as opposed to Linux, my system). The SO question that revealed this issue is [here|https://codereview.stackexchange.com/questions/174705/find-common-directory-path/174728#174728.]

The code:

import Foundation
func getPrefix(_ text:[String]) -> String? {
var common:String = text[0]
for i in text {
common = i.commonPrefix(with: common)
} return common 
}
var test = ["/home/user1/tmp/coverage/test", "/home/user1/tmp/covert/operator", "/home/user1/tmp/coven/members"]
var output:String = getPrefix(test)!
print(output)

Will generate the correct answer in Linux:

/home/user1/tmp

But a wrong result in OSx:

/home/user1/tmp/cove

Screenshots of the two results can be found in the SO discussion in the link above.

@belkadan
Copy link

belkadan commented Sep 5, 2017

The macOS answer looks correct to me. These are string operations, not path operations. @phausler?

@spevans
Copy link
Collaborator

spevans commented Aug 26, 2018

The macOS one is correct, the Linux version isn't actually a path operation, it just happens to be buggy using a binary search and the middle character it selects is the `p` or `tmp` so appears to give the correct answer.

#1668 should be a fix for `commonPrefix(with:options🙂` on Linux.

A possible solution to finding the common directory path is:

import Foundation

func getPrefix(_ text:[String]) -> String {
    var common = text[0]
    for i in text[1...] {
        common = i.commonPrefix(with: common)
    }
    if let i = common.lastIndex(of: "/") {
        common.removeSubrange(i..<common.endIndex)
    }
    return common
}

let test = ["/home/user1/tmp/coverage/test", "/home/user1/tmp/covert/operator", "/home/user1/tmp/coven/members"]
let output = getPrefix(test)
print(output)

@spevans
Copy link
Collaborator

spevans commented Sep 13, 2018

The PR has been merged into master now.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants