Bug 213588 - Add a new templated string type to help write more idiomatic parsing code
Summary: Add a new templated string type to help write more idiomatic parsing code
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: New Bugs (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Sam Weinig
URL:
Keywords: InRadar
Depends on:
Blocks: 213460
  Show dependency treegraph
 
Reported: 2020-06-24 19:29 PDT by Sam Weinig
Modified: 2020-06-25 17:22 PDT (History)
11 users (show)

See Also:


Attachments
Patch (25.23 KB, patch)
2020-06-24 19:38 PDT, Sam Weinig
no flags Details | Formatted Diff | Diff
Patch (20.86 KB, patch)
2020-06-25 10:29 PDT, Sam Weinig
no flags Details | Formatted Diff | Diff
Patch (21.09 KB, patch)
2020-06-25 13:27 PDT, Sam Weinig
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Sam Weinig 2020-06-24 19:29:20 PDT
Add a new templated string type to help write more idiomatic parsing code
Comment 1 Sam Weinig 2020-06-24 19:31:10 PDT
While working on https://bugs.webkit.org/show_bug.cgi?id=213460, it became clear having a typed string type that worked with our parsing idioms would be a nice code improvement. Breaking off that work into this bug.
Comment 2 Sam Weinig 2020-06-24 19:38:53 PDT Comment hidden (obsolete)
Comment 3 Sam Weinig 2020-06-25 10:29:32 PDT
Created attachment 402746 [details]
Patch
Comment 4 Darin Adler 2020-06-25 11:05:12 PDT
Comment on attachment 402746 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=402746&action=review

I would prefer if there was at least one use going in with the class.

> Source/WTF/wtf/text/StringParsingBuffer.h:42
> +    constexpr StringParsingBuffer()
> +    {
> +    }

Maybe = default for this one?

> Source/WTF/wtf/text/StringParsingBuffer.h:48
> +    }

Maybe: ASSERT(characters || !length)

> Source/WTF/wtf/text/StringParsingBuffer.h:54
> +    }

ASSERT(!characters == !end) so we don’t pass a null by accident.

> Source/WTF/wtf/text/StringParsingBuffer.h:57
> +    constexpr const CharacterType* position() const { return m_position; }
> +    constexpr const CharacterType* end() const { return m_end; }

Can these use auto instead of const CharacterType*?

> Source/WTF/wtf/text/StringParsingBuffer.h:67
> +        ASSERT(m_position + i < m_end);

Also ASSERT(m_position < m_end) to be overflow-proof?

> Source/WTF/wtf/text/StringParsingBuffer.h:73
> +        return *m_position;

ASSERT(m_position < m_end);

> Source/WTF/wtf/text/StringParsingBuffer.h:78
> +        ++m_position;

ASSERT(m_position < m_end);

> Source/WTF/wtf/text/StringParsingBuffer.h:83
> +        m_position += places;

ASSERT(m_position <= m_end);
ASSERT(m_position + places <= m_end);

> Source/WTF/wtf/text/StringParsingBuffer.h:95
> +        ++(*this);

No need for the parentheses.
Comment 5 Sam Weinig 2020-06-25 13:27:30 PDT
Created attachment 402803 [details]
Patch
Comment 6 Darin Adler 2020-06-25 13:41:03 PDT
Comment on attachment 402803 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=402803&action=review

> Source/WTF/wtf/text/StringParsingBuffer.h:54
> +        ASSERT(m_end >= m_position);
> +        ASSERT(!characters == !end);

Maybe also assert that the length fits in unsigned?

> Source/WTF/wtf/text/StringParsingBuffer.h:69
> +        ASSERT(m_position + i < m_end);

On reflection, because of overflow I think should be written like this:

    ASSERT(i < lengthRemaining());

> Source/WTF/wtf/text/StringParsingBuffer.h:75
> +        ASSERT(m_position < m_end);

New, better idea:

    ASSERT(hasCharactersRemaining());

> Source/WTF/wtf/text/StringParsingBuffer.h:81
> +        ASSERT(m_position < m_end);

Ditto.

> Source/WTF/wtf/text/StringParsingBuffer.h:85
> +    constexpr void advanceBy(unsigned places)

Wonder if this should be size_t instead of unsigned.

> Source/WTF/wtf/text/StringParsingBuffer.h:87
> +        ASSERT(m_position <= m_end);

I guess it was silly that I suggested this one. Obviously, this is the class's invariant, so we could assert this anywhere.

> Source/WTF/wtf/text/StringParsingBuffer.h:88
> +        ASSERT(m_position + places <= m_end);

On reflection, because of overflow I think should be written like this:

    ASSERT(places <= lengthRemaining());
Comment 7 EWS 2020-06-25 14:13:23 PDT
Committed r263529: <https://trac.webkit.org/changeset/263529>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 402803 [details].
Comment 8 Darin Adler 2020-06-25 15:12:54 PDT
Hope you don’t mind my after-commit comments!
Comment 9 Radar WebKit Bug Importer 2020-06-25 17:18:26 PDT
<rdar://problem/64781816>
Comment 10 Sam Weinig 2020-06-25 17:22:13 PDT
(In reply to Darin Adler from comment #8)
> Hope you don’t mind my after-commit comments!

Not in the slightest.
Comment 11 Radar WebKit Bug Importer 2020-06-25 17:22:28 PDT
<rdar://problem/64781954>