Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Merge pull request #9260 from leoetlino/fmt-checks
Common/Log: Add compile-time format string checks
- Loading branch information
Showing
7 changed files
with
91 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // Copyright 2020 Dolphin Emulator Project | ||
| // Licensed under GPLv2+ | ||
| // Refer to the license.txt file included. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstddef> | ||
| #include <string_view> | ||
|
|
||
| namespace Common | ||
| { | ||
| constexpr std::size_t CountFmtReplacementFields(std::string_view s) | ||
| { | ||
| std::size_t count = 0; | ||
| for (std::size_t i = 0; i < s.size(); ++i) | ||
| { | ||
| if (s[i] != '{') | ||
| continue; | ||
|
|
||
| // If the opening brace is followed by another brace, what we have is | ||
| // an escaped brace, not a replacement field. | ||
| if (i + 1 < s.size() && s[i + 1] == '{') | ||
| { | ||
| // Skip the second brace. | ||
| // This ensures that e.g. {{{}}} is counted correctly: when the first brace character | ||
| // is read and detected as being part of an '{{' escape sequence, the second character | ||
| // is skipped so the most inner brace (the third character) is not detected | ||
| // as the end of an '{{' pair. | ||
| ++i; | ||
| continue; | ||
| } | ||
|
|
||
| ++count; | ||
| } | ||
| return count; | ||
| } | ||
|
|
||
| static_assert(CountFmtReplacementFields("") == 0); | ||
| static_assert(CountFmtReplacementFields("{} test {:x}") == 2); | ||
| static_assert(CountFmtReplacementFields("{} {{}} test {{{}}}") == 2); | ||
| } // namespace Common |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters