Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize util/strencodings and util/string: string_view and optional #25001

Closed
wants to merge 11 commits into from

Conversation

maflcko
Copy link
Member

@maflcko maflcko commented Apr 27, 2022

Make use of std::string_view and std::optional in the util/{strencodings, string} files.

This avoids many temporary string/vector objects being created, while making the interface easier to read. Changes include:

  • Make all input arguments in functions in util/strencodings and util/string take std::string_view instead of std::string.
  • Add RemovePrefixView and TrimStringView which also return std::string_view objects (the corresponding RemovePrefix and TrimString keep returning an std::string, as that's needed in many call sites still).
  • Stop returning std::string from DecodeBase32 and DecodeBase64, but return vectors. Base32/64 are fundamentally algorithms for encoding bytes as strings; returning std::string from those (especially doing it conditionally based on the input arguments/types) is just bizarre.
  • Stop taking a bool* pf_invalid output argument pointer in DecodeBase32 and DecodeBase64; return an std::optional instead.
  • Make DecodeBase32 and DecodeBase64 more efficient by doing the conversion from characters to integer symbols on-the-fly rather than through a temporary vector.

sipa and others added 11 commits April 27, 2022 14:12
In addition, to make sure that no call site ignores the invalid
decoding status, make the pf_invalid argument mandatory.
Base32/base64 are mechanisms for encoding binary data. That they'd
decode to a string is just bizarre. The fact that they'd do that
based on the type of input arguments even more so.
-BEGIN VERIFY SCRIPT-
 sed -i 's,ValidAsCString,ContainsNoNUL,g' $(git grep -l ValidAsCString)
-END VERIFY SCRIPT-
@maflcko
Copy link
Member Author

maflcko commented Apr 27, 2022

Disclaimer: The description and most commits are stolen from #24764 (comment)

@maflcko
Copy link
Member Author

maflcko commented Apr 27, 2022

Previous review: #24764 (review)

re-ACK fa7078d only change is rebase and adding a scripted-diff 🍲

Show signature

Signature:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

re-ACK fa7078d84fc2858a466bc1a85404f821df682538 only change is rebase and adding a scripted-diff 🍲
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUjYUQv/TURxeLbk3gryj4kDPgdoqT5a/1EOIBJ7kAM9bT/jzpxuqM9ZNOGSlIxu
DmmVgwGwMxvxE0fd7WjxOrdYTSTIQfMwlPAJpSR3JCTHvh/xI1ew1Xmjq1iRTgcY
6ma3XMavD9X54VbpH2tlMRj3F/Vhb5MHAQiJUU3v3vImst8BL+HfwB3fsXkYlsPe
F5auWj58clyrtGRed5HnL4UDKLmLg95S/9jSUFh/6sIgxw7QNWbXhnNxtBDjuOLw
EDpZd9NmhDBqmq6dx9wPQMEoYMDjeICXLciMzoUU6WI+Y7UuK5kdHrF+Y6/Jogqc
Q9CLGkBOxUOACc+i115Q+SKykiibphLYvIrbSxv8etjbB4XFQNvJKL96UCUP9pa5
h4vy/k9V2hspW9KRoazJevv90rb3m7sKG7yqRr4IurWcfTWW+oPv3Wff9uXbd4oQ
AfQg2ql9wCHcC+3T/1o9GDJOkSf1xCDUCD/wReekhr8JuLiJhVvg9bUkUuPwOUa0
Ddfn99lR
=dVEV
-----END PGP SIGNATURE-----

@martinus
Copy link
Contributor

Code review ACK fa7078d, found no issue

@laanwj
Copy link
Member

laanwj commented Apr 27, 2022

Code review ACK fa7078d

@fanquake fanquake requested a review from laanwj April 27, 2022 14:39
@sipa
Copy link
Member

sipa commented Apr 27, 2022

utACK fa7078d (as far as the commit that isn't mine goes)

laanwj added a commit to bitcoin-core/gui that referenced this pull request Apr 27, 2022
…ing: `string_view` and `optional`

fa7078d scripted-diff: Rename ValidAsCString to ContainsNoNUL (MacroFake)
e7d2fbd Use std::string_view throughout util strencodings/string (Pieter Wuille)
8ffbd14 Make DecodeBase{32,64} take string_view arguments (Pieter Wuille)
1a72d62 Generalize ConvertBits to permit transforming the input (Pieter Wuille)
78f3ac5 Make DecodeBase{32,64} return optional instead of taking bool* (Pieter Wuille)
a65931e Make DecodeBase{32,64} always return vector, not string (Pieter Wuille)
a4377a0 Reject incorrect base64 in HTTP auth (Pieter Wuille)
d648b51 Make SanitizeString use string_view (Pieter Wuille)
963bc9b Make IsHexNumber use string_view (Pieter Wuille)
4006299 Make IsHex use string_view (Pieter Wuille)
c1d165a Make ParseHex use string_view (Pieter Wuille)

Pull request description:

  Make use of `std::string_view` and `std::optional` in the util/{strencodings, string} files.

  This avoids many temporary string/vector objects being created, while making the interface easier to read. Changes include:
  * Make all input arguments in functions in util/strencodings and util/string take `std::string_view` instead of `std::string`.
  * Add `RemovePrefixView` and `TrimStringView` which also *return* `std::string_view` objects (the corresponding `RemovePrefix` and `TrimString` keep returning an `std::string`, as that's needed in many call sites still).
  * Stop returning `std::string` from `DecodeBase32` and `DecodeBase64`, but return vectors. Base32/64 are fundamentally algorithms for encoding bytes as strings; returning `std::string` from those (especially doing it conditionally based on the input arguments/types) is just bizarre.
  * Stop taking a `bool* pf_invalid` output argument pointer in `DecodeBase32` and `DecodeBase64`; return an `std::optional` instead.
  * Make `DecodeBase32` and `DecodeBase64` more efficient by doing the conversion from characters to integer symbols on-the-fly rather than through a temporary vector.

ACKs for top commit:
  MarcoFalke:
    re-ACK fa7078d only change is rebase and adding a scripted-diff 🍲
  martinus:
    Code review ACK fa7078d, found no issue
  laanwj:
    Code review ACK  fa7078d
  sipa:
    utACK fa7078d (as far as the commit that isn't mine goes)

Tree-SHA512: 5cf02e541caef0bcd100466747664bdb828a68a05dae568cbcd0632a53dd3a4c4e85cd8c48ebbd168d4247d5c9666689c16005f1c8ad75b0f057d8683931f664
@fanquake fanquake closed this Apr 27, 2022
@maflcko maflcko deleted the 2204-span-stuff-🐘 branch April 27, 2022 15:43
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Apr 29, 2022
maflcko pushed a commit to bitcoin-core/gui that referenced this pull request Dec 21, 2022
…iterator

bb5ea1d qt: Load PSBTs using istreambuf_iterator rather than istream_iterator (Andrew Chow)

Pull request description:

  `istream_iterator` eats whitespace charactesr which causes parsing failures for PSBTs that contain the bytes corresponding to those characters. `istreambuf_iterator` is the correct thing to use here.

  This is a regression in 24.0. bitcoin/bitcoin#25001 accidentally changed the original `istreambuf_iterator` to `istream_iterator`.

ACKs for top commit:
  furszy:
    Tested ACK bb5ea1d
  MarcoFalke:
    review ACK bb5ea1d   🍇

Tree-SHA512: 35d90eee3efdcb6a360af69ac1727f9f2837ea621297196de3136299f5de6d9975df4e425e1fc5b8813c1ddb2a4d60c3969e1d5d968953a4628ca45e37d3bf05
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Dec 21, 2022
…er than istream_iterator

bb5ea1d qt: Load PSBTs using istreambuf_iterator rather than istream_iterator (Andrew Chow)

Pull request description:

  `istream_iterator` eats whitespace charactesr which causes parsing failures for PSBTs that contain the bytes corresponding to those characters. `istreambuf_iterator` is the correct thing to use here.

  This is a regression in 24.0. bitcoin#25001 accidentally changed the original `istreambuf_iterator` to `istream_iterator`.

ACKs for top commit:
  furszy:
    Tested ACK bb5ea1d
  MarcoFalke:
    review ACK bb5ea1d   🍇

Tree-SHA512: 35d90eee3efdcb6a360af69ac1727f9f2837ea621297196de3136299f5de6d9975df4e425e1fc5b8813c1ddb2a4d60c3969e1d5d968953a4628ca45e37d3bf05
@bitcoin bitcoin locked and limited conversation to collaborators Apr 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants