Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Issue 29714586: Noissue - implement a couple of common lexical_cast cases. (Closed)

Created:
March 5, 2018, 3:32 p.m. by sergei
Modified:
March 6, 2018, 4:23 p.m.
Base URL:
github.com:adblockplus/adblockpluscore
Visibility:
Public.

Description

Add `lexical_cast converting a String value into an integer, Boolean or OwnedString. So far the implementation is the bare minimum required to have it, in particular to let one implement https://codereview.adblockplus.org/29548581/ and https://codereview.adblockplus.org/29606600/, where it was extracted from. Review: https://codereview.adblockplus.org/29714586

Patch Set 1 #

Total comments: 4

Patch Set 2 : address comment #

Total comments: 3
Unified diffs Side-by-side diffs Delta from patch set Stats (+137 lines, -0 lines) Patch
M compiled/String.h View 1 2 chunks +73 lines, -0 lines 3 comments Download
M test/compiled/String.cpp View 1 1 chunk +64 lines, -0 lines 0 comments Download

Messages

Total messages: 6
sergei
March 5, 2018, 3:43 p.m. (2018-03-05 15:43:38 UTC) #1
hub
https://codereview.adblockplus.org/29714586/diff/29714593/compiled/String.h File compiled/String.h (right): https://codereview.adblockplus.org/29714586/diff/29714593/compiled/String.h#newcode496 compiled/String.h:496: for (; pos < len && value[pos] >= u'0' ...
March 5, 2018, 7:24 p.m. (2018-03-05 19:24:49 UTC) #2
sergei
https://codereview.adblockplus.org/29714586/diff/29714593/compiled/String.h File compiled/String.h (right): https://codereview.adblockplus.org/29714586/diff/29714593/compiled/String.h#newcode496 compiled/String.h:496: for (; pos < len && value[pos] >= u'0' ...
March 6, 2018, 10:36 a.m. (2018-03-06 10:36:27 UTC) #3
hub
https://codereview.adblockplus.org/29714586/diff/29715625/compiled/String.h File compiled/String.h (right): https://codereview.adblockplus.org/29714586/diff/29715625/compiled/String.h#newcode506 compiled/String.h:506: bool isDangerous = pos >= std::numeric_limits<T>::digits10; this check on ...
March 6, 2018, 2:45 p.m. (2018-03-06 14:45:53 UTC) #4
sergei
https://codereview.adblockplus.org/29714586/diff/29715625/compiled/String.h File compiled/String.h (right): https://codereview.adblockplus.org/29714586/diff/29715625/compiled/String.h#newcode506 compiled/String.h:506: bool isDangerous = pos >= std::numeric_limits<T>::digits10; On 2018/03/06 14:45:53, ...
March 6, 2018, 3:41 p.m. (2018-03-06 15:41:32 UTC) #5
hub
March 6, 2018, 3:56 p.m. (2018-03-06 15:56:45 UTC) #6
LGTM

https://codereview.adblockplus.org/29714586/diff/29715625/compiled/String.h
File compiled/String.h (right):

https://codereview.adblockplus.org/29714586/diff/29715625/compiled/String.h#n...
compiled/String.h:506: bool isDangerous = pos >=
std::numeric_limits<T>::digits10;
On 2018/03/06 15:41:32, sergei wrote:
> On 2018/03/06 14:45:53, hub wrote:
> > this check on the number of digits could also be done before the loop: if we
> > have more digits (`len` or `len - 1` if it has a sign) than the maximum it
is
> an
> > error.
> 
> This condition is satisfied but from a different perspective and the idea of
> having isDangerous here is rather for optimization, namely in order to skip
> overflow checks when there is no necessity in them.
> 
> std::numeric_limits<T>::digits10 is the maximum number of digits which can be
> stored in an integer type T without loss. E.g. for int8_t the range is [-128,
> 127], so digits10 is equal to 2, it can safely store [-99, 99], similar for
> other types. So while the length of a text represented number is less than
> `digits10 + 1` it's safe to multiply it by 10 and add `digit`, however when
the
> length reaches digits10 one should check that there is no overflow. E.g.
`value
> = "300"`, `result == 30`, `pos = 2`, now we need to check that there is no
> overflow before multiplying it by 10, in that example there is an overflow.
> Another example is when the `value = 128`, it's safe to multiply 12 by 10 but
> one should not add 8, one may add only a number from [0, 7]. Just in case,
it's
> covered in tests, but only for (u)int32_t.
> 
> If the number exceeds the valid range, e.g. "1111" for int8_t it will fail on
> the check before multiplying by 10, `127 / 10 < 111` is true. It's also
covered
> in the tests.
> 
> It seems not a common case when a number is too big for parsing, so IMO we
> should not check it before the for-loop additionally.
> 
> BTW the implementation could also take into account '-' but I tried to not
> introduce additional conditions, so there is no such check because I thought
> that negative numbers are not too common in our data either. BTW, because of
> similar reasons there is a support of zeros at the beginning, there is just no
> additional check for them and as the result there is such feature.
> 

Acknowledged.

Powered by Google App Engine
This is Rietveld