I have a file containing information that I want to load in the application. The file has some header infos as string, then multiple entries that are ended by ';' Some entries are used for different types and therefore lenght is variable, but all variables are separated by ','
Example:
\Some heading
\Some other heading
I,003f,3f3d00ed,"Some string",00ef,
0032,20f3
;
Y,02d1,0000,0000,"Name of element",
00000007,0,
00000000,0,
;
Y,02d1,0000,0000,"Name of element",30f0,2d0f,02sd,
00000007,0,
00000000,0,
;
I is one type of element Y is another type of element
What I want to achieve is, to bring the elements into different structs to work with. Most of the values are numbers but some are strings.
What I was able to achieve is:
- Import the file as Vec<u8>
- Put it in a string (can't do that directly, beacuse there may be UTF-8 problems in elements I'm not interested in)
- Split it to a Vec<&str> by ';'
- Pass the strings to functions depending on their type
- Split it to a Vec by '\n'
- Split it to a Vec by ','
- Reading out the data I need and interpret from the strings (str::from_str_radix for example)
- Buld the struct and return it
This seems not to be the way to go, since I start with bytes, allocate them as string and then again allocate numbers on most of the values.
So my question is:
Can I split the Vec<u8> into multiple vectors separated by ';' (byte 59), split these further by '\n' and split this further by ','. I assume it would be more performant to apply the bytes directly to the correct data-type. Or is my concern wrong?
Split
so you'd have to invent a scheme to pair the split separator around in order to know your 3 levels of boundaries, which is basically ad-hoc parsing except bad. – Masklinn