Hi, I'm bumping into an issue with Okio's `Buffere...
# squarelibraries
a
Hi, I'm bumping into an issue with Okio's
BufferedSource.select()
and
TypedOptions
. It seems like the options selects the shortest match,. E.g. when a source starts with
fffe0000
it will always select the
fffe
option, but I want it to select
fffe0000
. I can implement something manually, but I wanted to check first: is this the intended behaviour?
j
Probably? Usually we have a terminating discriminator that disambiguates prefixes of other items, such as in JSON we would select
lunch"
and
lunchbox"
allowing the closing quote to force the prefix to otherwise no longer be a prefix.
You can file an issue about it. I'm not sure whether it's something we can change or not. It might require new API surface.
j
Sort your options by the order you’d prefer the match to be in
Like so:
Copy code
private val UnicodeBomOptions: TypedOptions<CharEncoding> =
  TypedOptions.of(CharEncoding.entries.sortedBy { it.bom }.reversed()) {
    when (it) {
      CharEncoding.UTF_8 -> it.bom
      CharEncoding.UTF_16BE -> it.bom
      CharEncoding.UTF_16LE -> it.bom
      CharEncoding.UTF_32BE -> it.bom
      CharEncoding.UTF_32LE -> it.bom
    }
  }
...reverse sorting will be prefer-longest; forward sorting will be prefer-shortest
good thing no one uses that massively wasteful encoding
j
whoa
that’s embarrassing
j
be is wrong too
wtf was i smoking
j
BE is right?
FE FF
j
the utf-32 one is wrong
j
oh, yep
0xFFFFuuuu
j
need laugh/cry/sob emoji here
laugh cry face palm 1
a
Sort your options by the order you’d prefer the match to be in
Ahhhh right, sort the enum entries! That works, thanks!
It looks like there might be a few other places with the wrong BOM? https://github.com/search?q=org%3Asquare+ffff0000+language%3AKotlin&type=code
quick PRs (made in the web-gui - I might have missed something) • https://github.com/square/wire/pull/2935https://github.com/square/okhttp/pull/8403
j
The order needs to change per the above discussion
1