News for hackers. Stuff that matters.
Re:Custom string delimiters in Swift 5 by saagarjha on Thursday December 27, 2018 @05:46PM (#18773327)
> split a filename into parts

This is not something strings should do; you should be using URL instead. Foundation realized this long ago and has started deprecating string-based path APIs because treating a path as a string exposes fundamental misconceptions very quickly.

> extract regex matches

This is something that is being worked on/discussed, but has not been “solved” yet.

Re:Custom string delimiters in Swift 5 by jakobegger on Thursday December 27, 2018 @06:31PM (#18773612)
> This is not something strings should do;

I didn't mean that String should have a "pathExtension" property like NSString. I just want operations like "insert a number before the last period" to be easier to do.

Also, there are a lot more things that are a pain with String. I mean, just the fact that I can't pass a Substring to a function that expects a String boggles my mind.

Re:Custom string delimiters in Swift 5 by a1369209993 on Thursday December 27, 2018 @10:00PM (#18774580)
> I just want operations like "insert a number before the last period" to be easier to do.

Stuff.tar1.gz would like to have a word with you.

Re:Custom string delimiters in Swift 5 by saagarjha on Thursday December 27, 2018 @07:11PM (#18773820)
Yes, like I said, you should be using URL for this, which will make it possible to extract the filename without the extension, modify it, and get the path string from that (which is semantically what I believe you are trying to do). Not only does this create self-documenting code, it also makes it harder to fall into a the many “gotchas” that handling paths entails.
Re:Custom string delimiters in Swift 5 by jakobegger on Friday December 28, 2018 @02:13AM (#18775295)
Yes, I know that, but I was just using filename manipulation as an example, because it was the first thing that came to my mind. I now see it was a bad example, since URL has a couple of APIs for file name handling.

Anyway, these types of operations come up all the time when working with strings, and these operations are really really incovenient in Swift.

Re:Custom string delimiters in Swift 5 by saagarjha on Friday December 28, 2018 @04:11AM (#18775707)
> Anyway, these types of operations come up all the time when working with strings, and these operations are really really incovenient in Swift.

Again, what are these operations? I think most of the issues people have with String is due to hard to discover APIs rather than design issues with String.

Re:Custom string delimiters in Swift 5 by jakobegger on Friday December 28, 2018 @12:54PM (#18778253)
Text parsing is something that comes up often (for me). For example parsing CSV files, or parsing tab separated data from the clipboard, or parsing various types of config files.

Swift has sweet solutions for a few special cases (eg. parse file names, parse dates, parse JSON), but parsing generic text is not something that Swift is good at.

NSString has a rich set of APIs for those tasks (NSScanner, NSRegularExpression, and all the rangeOfString... methods). Swift has pretty much nothing. (There is firstIndex(of:), but as far as I can see there is no way to find the next index, which makes it pretty much useless for most parsing tasks)

Maybe the problem is that I'm just not familiar enough with Swifts String, but until now every time I've had to work with Strings I became very frustrated.

Re:Custom string delimiters in Swift 5 by tinus_hn on Friday December 28, 2018 @03:38PM (#18779234)
I would strongly suggest that you don’t write a parser for .csv yourself, it’s a terrible format.
Re:Custom string delimiters in Swift 5 by arcticbull on Thursday December 27, 2018 @10:59PM (#18774792)
Why would URL be needed to handle file system only paths? They’re different and OS Specific. Rusts Path vs String vs third-party URL strikes the right balance IMO. It’s a third thing.
Re:Custom string delimiters in Swift 5 by saagarjha on Friday December 28, 2018 @04:12AM (#18775713)
Foundation’s URL handles file system paths, among other things. Thus, it is the right choice to do path-based operations.
Re:Custom string delimiters in Swift 5 by arcticbull on Saturday December 29, 2018 @12:48PM (#18784745)
Sorta, it's the right choice on iOS and macOS but conceptually filesystem paths are different than URIs even though the same information can be encoded into both. It's weird to have URIs pointing at google (technically valid by means of being the same type) being passed to file-management APIs - only to have them unsurprisingly fail. It subverts the type system by allowing invalid blobs to avoid being caught by the type system. Paths as URIs in contexts where only FS paths are valid is akin to any other stringly-typed API. A third type is more logical: URL, String and FsPath.
Re:Custom string delimiters in Swift 5 by blitmap on Thursday December 27, 2018 @06:23PM (#18773564)
Are you saying just exploding a URL into its components?
Re:Custom string delimiters in Swift 5 by saagarjha on Thursday December 27, 2018 @06:30PM (#18773610)
I don’t understand your question. If it is “can I explode a URL into components”, the answer is that URL can do this.
Re:Custom string delimiters in Swift 5 by duncan-donuts on Thursday December 27, 2018 @08:12PM (#18774105)
I’m not sure about the specifics with swift, but I thinking he’s referring to a type, URL. Ruby has a URI module that you would use to work with uri strings, and I think that’s exactly what he is taking about.