So I have the following problem, in YAML single quotes are a strange beast.
Short primer: a YAML single quotes spans one line, and has no escape character. Instead the '
character is doubled (''
) to escape it.
'I''m a string' # parses to Scalar value of "I'm a string"
However, if the string is empty, then it’s not treated as escape.
'' # parses to Scalar value "" (empty string)
Which can be a problem. Consider following line:
{'':''''} # parses to Map {"": "''"}
When applied to a bitmask you get
bytes | { |
' |
' |
: |
' |
' |
' |
' |
} |
---|---|---|---|---|---|---|---|---|---|
bitmask | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 1 | 0 |
relevant quotes | 1 | 1 | 1 | 1 |
But in another example
''' ' # Parses to Scalar "'' "
Gets following bitmaks
bytes | ' |
|
' |
' |
' |
---|---|---|---|---|---|
bitmask | 1 | 0 | 1 | 1 | 1 |
relevant quotes | 1 | 1 |
In second example the 3rd and 4th byte are ignored because they are inside an existing single quote string i.e they are treated as ''
escape for '
. And this logic depends on how many single quotes were beforehand.
How to express this logic branchlessly if bit state depends on previous bytes being in/out single quote?
I tried several ways to calculate, theses bit values, but I’m kinda stuck, the furtherest I got was logic that can select the bits correctly in case #1. Logic is bellow in pseudo Rust code
fn find_relevant_bytes(bytes: u8) -> u8 {
let low_part = bytes & bytes >> 1;
let high_part = bytes & bytes << 1;
return low_part ^ high_part;
}