S7NetPlus / s7netplus

S7.NET+ -- A .NET library to connect to Siemens Step7 devices
MIT License
1.29k stars 577 forks source link

Return expected length substring rather than throw an exception when reading strings #500

Open Himmelt opened 1 year ago

Himmelt commented 1 year ago

Return expected length substring rather than throw an exception when reading strings

mycroes commented 1 year ago

HI @Himmelt,

First off, thanks for your contribution. I'm not sure what to think about this change. While the remark with regards to reading a single string sounds quite fair, this doesn't work in different contexts. When someone attempts to read a string as part of a struct, the incorrect length will lead to a whole new set of errors that might go on undetected.

I'd prefer to have a separate bool TryGetString(byte[] input, out string result, out int size) instead, that would return true iff the full string can be read from input and sets result and size to their appropriate values, or false when input doesn't contain enough bytes, setting result to the partial decoded string and still assigning size to the reserved size for the string.

If you want to implement the above functionality feel free to do that here or in a new PR, I prefer to have the current commits dropped entirely instead of reverted though.