Is this an acceptable use of "ASCII arithmetic"?

I have a string value of the form 10123X123456 , where 10 is the year, 123 is the number of the day in the year, and the rest is a unique system. In certain circumstances, I need to add 400 to the day number, so the number above, for example, becomes 10523X123456 .

My first idea was to fine-tune these three characters, convert them to an integer, add 400 to it, convert them back to a string, and then call replace in the original string. It works.

But then it occurred to me that the only character that I really needed to change was the third, and that the original value would always be 0-3, so there would never be any problems with hyphenation. It also occurred to me that the ASCII code for numbers is sequential, therefore, for example, adding the number 4 to the character โ€œ0โ€ will result in โ€œ4โ€, etc. So what I finished doing.

My question is, is there some reason that will not always work? I usually avoid "ASCII arithmetic" on the grounds that it is not cross-platform or internationalist. But it seems reasonable to assume that the code points for numbers will always be sequential, i.e. "4" will always be 1 more than "3". Does anyone see any problems with this reasoning?

Here is the code.

 string input = "10123X123456"; input[2] += 4; //Output should be 10523X123456 
+4
source share
5 answers

From the C ++ standard, section 2.2.3:

In both basic sets of source and executable values, the value of each character after 0 in above the list of decimal digits must be greater than the value of the previous one.

So yes, if you are guaranteed to never need a carry, you are good to go.

+9
source

C ++ language definition requires that the code point values โ€‹โ€‹of digits be consistent. Therefore, ASCII arithmetic is quite acceptable.

+5
source

Always remember that if this is generated by something that you do not have full control (for example, users and a third-party system), something may and will go wrong. (Check out Murphy's laws)

Therefore, I think that you should at least do some checks before doing this.

+3
source

It seems that changing the line as you described is easier than parsing the number in the first place. Therefore, if your algorithm works (and it certainly does what you describe), I would not consider it a premature optimization.

Of course, after adding 400, this is no longer the number of days, so you cannot apply this process recursively.

And, < mandatory warning of 2100 > .

+1
source

A long time ago I saw x86 instructions for ASCII and BCD . These are AAA (ASCII Adjust for Addition), AAS (subtraction), AAM (multi), AAD (div).
But even if you are not sure about the target platform, you can refer to the specification of the character set that you use, and I think you will find that the first 127 ASCII characters always have the same value for all character sets (for the first unicode, which is the first characters).

+1
source

Source: https://habr.com/ru/post/1304931/


All Articles