Replace non-alphanumeric, non-underscore characters in filewrapper

PiperOrigin-RevId: 246320238
Change-Id: I08454dc19b6227e4ce2c1b7677b916706e7be5a5
This commit is contained in:
Christian Blichmann 2019-05-02 08:11:21 -07:00 committed by Copybara-Service
parent 64cfb949f4
commit 7753cded13

View File

@ -14,6 +14,7 @@
// Simple utility to wrap a binary file in a C++ source file. // Simple utility to wrap a binary file in a C++ source file.
#include <algorithm>
#include <cstdint> #include <cstdint>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
@ -216,9 +217,10 @@ int main(int argc, char* argv[]) {
{ // Write header file first. { // Write header file first.
File out_h(*arg++, "wb"); File out_h(*arg++, "wb");
--argc; --argc;
std::string header_guard = absl::StrReplaceAll( std::string header_guard = absl::StrFormat("%s_%s_H_", package, toc_ident);
absl::AsciiStrToUpper(absl::StrFormat("%s_%s_H_", package, toc_ident)), std::replace_if(
{{"/", "_"}}); header_guard.begin(), header_guard.end(),
[](char c) { return !absl::ascii_isalnum(c); }, '_');
absl::FPrintF(out_h.get(), kHFileHeaderFmt, package, toc_ident, absl::FPrintF(out_h.get(), kHFileHeaderFmt, package, toc_ident,
header_guard); header_guard);
if (have_ns) { if (have_ns) {
@ -248,8 +250,10 @@ int main(int argc, char* argv[]) {
File in(in_filename, "rb"); File in(in_filename, "rb");
std::string basename = sandbox2::file_util::fileops::Basename(in_filename); std::string basename = sandbox2::file_util::fileops::Basename(in_filename);
std::string ident = absl::StrCat( std::string ident = absl::StrCat("k", basename);
"k", absl::StrReplaceAll(basename, {{".", "_"}, {"-", "_"}})); std::replace_if(
ident.begin(), ident.end(),
[](char c) { return !absl::ascii_isalnum(c); }, '_');
absl::FPrintF(out_cc.get(), kCcDataBeginFmt, ident); absl::FPrintF(out_cc.get(), kCcDataBeginFmt, ident);
// Remember identifiers, they are needed in the kToc array. // Remember identifiers, they are needed in the kToc array.
toc_entries.emplace_back(std::move(basename), std::move(ident)); toc_entries.emplace_back(std::move(basename), std::move(ident));