pixel,
@pixel@social.pixels.pizza avatar

Swift debugging driving me nuts. #helpMe

I have a multiline file that I’ve loaded into a string using String(contentsOfFile:encoding:)

I'm trying to split the string using “someString.components(separatedBy: "==========\n“)

But it... just doesn’t. I know that particular string does exist in the file multiple times.

If I change the separator to just a linefeed it does split the file into 1600 items just fine. So for some reason it's not finding the "==========\n“ ?

I'm stumped.

GeekAndDad,
@GeekAndDad@mastodon.social avatar

@pixel Any luck with this?

pixel,
@pixel@social.pixels.pizza avatar

@GeekAndDad @krzyzanowskim ok, just started on it again, and SUCCESS -

  1. There are definitely UTF16 BOM in the file.
  2. if I read the file as .utf16, it doesn't work.
  3. If I read the file as .utf8, AND strip out the UTF16 BOM, BINGO.

let cleanedClippings = clippings.replacingOccurrences(of: "\u{feff}", with: "”)

Each entry has a UTF16 marke? who knows. 332 of them in my file! I grabbed it off my kindle again just to see if I had somehow done it, but no that's how it is.

Thank you all!

gregtitus,
@gregtitus@social.coop avatar

@pixel @GeekAndDad @krzyzanowskim It looks like each individual clipping was originally a separate text file in utf-16 (which you would expect would start with a BOM), but then whatever did the combining wasn't aware and did either raw binary concatenation or expected utf-8 or some other encoding like that.

pixel,
@pixel@social.pixels.pizza avatar

@gregtitus @GeekAndDad @krzyzanowskim Thanks for noticing the UTF16 BOM Greg!!

mredig,
@mredig@fosstodon.org avatar

@pixel @gregtitus @GeekAndDad @krzyzanowskim cough :morty: https://fosstodon.org/@mredig/112469938386709317

But I’m just being petty. Greg is definitely the real hero :)

pixel,
@pixel@social.pixels.pizza avatar
krzyzanowskim,
@krzyzanowskim@mastodon.social avatar

deleted_by_author

  • Loading...
  • pixel,
    @pixel@social.pixels.pizza avatar
    krzyzanowskim,
    @krzyzanowskim@mastodon.social avatar

    deleted_by_author

  • Loading...
  • pixel,
    @pixel@social.pixels.pizza avatar

    @krzyzanowskim uhhhh wat

    GeekAndDad,
    @GeekAndDad@mastodon.social avatar

    @krzyzanowskim What Xcode/Swift are you using? (15.2, Swift 5.9.2 here).

    Starting to wonder if @pixel is on an alpha Xcode or something weird like that…

    pixel,
    @pixel@social.pixels.pizza avatar

    ok, thanks to both of you for digging into this. has to be something with my system. Even trying to just load the file and do components:separatedBy on it gives me the same issue. I'll have to look into this more later.

    GeekAndDad,
    @GeekAndDad@mastodon.social avatar
    krzyzanowskim,
    @krzyzanowskim@mastodon.social avatar

    deleted_by_author

  • Loading...
  • pixel,
    @pixel@social.pixels.pizza avatar

    @krzyzanowskim yeah, I copied it straight from the file.

    krzyzanowskim,
    @krzyzanowskim@mastodon.social avatar

    deleted_by_author

  • Loading...
  • pixel,
    @pixel@social.pixels.pizza avatar

    @krzyzanowskim yup. just tried that

    GeekAndDad,
    @GeekAndDad@mastodon.social avatar

    @pixel What value are you using for encoding:?

    pixel,
    @pixel@social.pixels.pizza avatar

    @GeekAndDad .utf8

    interesting thought.

    pixel,
    @pixel@social.pixels.pizza avatar

    @GeekAndDad ok, that's not it. tried a few others and they didn't work. opened in BBEdit and resaved as UTF8 just to make sure that's what it is, and still get the error. WEIRD.

    GeekAndDad,
    @GeekAndDad@mastodon.social avatar

    @pixel Would “=\n” work?

    pixel,
    @pixel@social.pixels.pizza avatar

    @GeekAndDad Nope! tried that too.

    GeekAndDad,
    @GeekAndDad@mastodon.social avatar

    @pixel Guessing the equal signs are different Unicode than you are typing into the source.

    Also wondering about a nonbreaking space or something between the = and the newline.

    Only other idea is escaping the “=“ (shouldn’t need to that I’m aware of but ¯_(ツ)_/¯). So:

    “=\n”

    But that seems very weird.

    pixel,
    @pixel@social.pixels.pizza avatar

    @GeekAndDad it's definitely the same character - I thought the same thing so I copied it straight from the file. There's no other invisible characters (turned on invisible chars in BBEdit).

    Can't escape it like that, compiler yells (as it should)

    its so weird.

    fun new clue: changing the separator to just “=" crashes with the error "Swift/ContiguousArrayBuffer.swift:600: Fatal error: Index out of range”

    Also, this is Swift running as a command line script (i.e. with hash bang on first line)

    GeekAndDad,
    @GeekAndDad@mastodon.social avatar

    @pixel I was going to recommend looking at it in BBedit :)

    Possible the editor is converting the character (normalizing it or something?) or the clipboard isn’t transferring it without conversion.

    I’d next hexdump the file and look at the bytes.

    pixel,
    @pixel@social.pixels.pizza avatar

    @GeekAndDad LOL ok, that was a good idea.

    it's /r/n. I guess BBEdit shows the same character for /r/n and just /n EXCEPT if I do ""=\r\n" it crashes with the "Swift/ContiguousArrayBuffer.swift:600: Fatal error: Index out of range” error

    GeekAndDad,
    @GeekAndDad@mastodon.social avatar

    @pixel oh doh!

    separatedBy: takes a CharacterSet, not a string constant.

    GeekAndDad,
    @GeekAndDad@mastodon.social avatar

    @pixel oh, no, there’s another definition in the protocol on a generic T. my bad.

    pixel,
    @pixel@social.pixels.pizza avatar

    @GeekAndDad haha ya

    pixel,
    @pixel@social.pixels.pizza avatar

    @GeekAndDad the definition for it is "func components<T>(separatedBy separator: T) -> [String] where T : StringProtocol”

    GeekAndDad,
    @GeekAndDad@mastodon.social avatar

    @pixel Using your test file gist pasted into BBEdit doc works for me.

     let s = try String(contentsOfFile: "/Users/dad/Downloads/testfile.txt",  
     encoding: .utf8)  
     let lines = s.components(separatedBy: "=\n")  
     print( "\(lines.count)")  
    

    -> 4

    pixel,
    @pixel@social.pixels.pizza avatar

    @GeekAndDad what the hell

    GeekAndDad,
    @GeekAndDad@mastodon.social avatar

    @pixel Just for giggles, try changing to NSString?

    // let s = try NSString(contentsOfFile: "/Users/dad/Downloads/testfile.txt",  
    // encoding: NSUTF8StringEncoding)
    
    and the rest?
    
    pixel,
    @pixel@social.pixels.pizza avatar

    @GeekAndDad No luck.

    GeekAndDad,
    @GeekAndDad@mastodon.social avatar

    @pixel In Xcode Version 15.2 because macOS 13.6.6 on this M1 MBP.

    I’m in a macOS SwiftUI app because that was the easiest to do a quick test in for me,
    so import SwiftUI at the top.

    pixel,
    @pixel@social.pixels.pizza avatar

    @GeekAndDad hmm. I'm on Xcode 15.4 interesting

  • All
  • Subscribed
  • Moderated
  • Favorites
  • mentalhealth
  • DreamBathrooms
  • ngwrru68w68
  • modclub
  • magazineikmin
  • thenastyranch
  • rosin
  • khanakhh
  • InstantRegret
  • Youngstown
  • slotface
  • Durango
  • kavyap
  • mdbf
  • GTA5RPClips
  • provamag3
  • ethstaker
  • normalnudes
  • tester
  • osvaldo12
  • everett
  • cubers
  • tacticalgear
  • anitta
  • megavids
  • Leos
  • cisconetworking
  • JUstTest
  • lostlight
  • All magazines