From 814a9e980a05821751a94c5bef7f1281d3a4a321 Mon Sep 17 00:00:00 2001 From: JeanElsner Date: Sat, 8 Jul 2023 20:59:41 +0200 Subject: [PATCH 1/2] Replace newlines in arg values with space --- include/pybind11/pybind11.h | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index decb340f98..0c04d33b82 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -52,6 +52,33 @@ PYBIND11_WARNING_DISABLE_MSVC(4127) PYBIND11_NAMESPACE_BEGIN(detail) +std::string replaceNewlinesAndSquash(const char* text) { + std::string result; + + // Replace newlines with spaces and squash consecutive spaces + while (*text) { + if (*text == '\n') { + result += ' '; + while (*(text + 1) == ' ') { + ++text; + } + } else { + result += *text; + } + ++text; + } + + // Strip leading and trailing spaces + result.erase(result.begin(), std::find_if(result.begin(), result.end(), [](unsigned char ch) { + return !std::isspace(ch); + })); + result.erase(std::find_if(result.rbegin(), result.rend(), [](unsigned char ch) { + return !std::isspace(ch); + }).base(), result.end()); + + return result; +} + // Apply all the extensions translators from a list // Return true if one of the translators completed without raising an exception // itself. Return of false indicates that if there are other translators @@ -424,7 +451,7 @@ class cpp_function : public function { // Write default value if available. if (!is_starred && arg_index < rec->args.size() && rec->args[arg_index].descr) { signature += " = "; - signature += rec->args[arg_index].descr; + signature += replaceNewlinesAndSquash(rec->args[arg_index].descr); } // Separator for positional-only arguments (placed after the // argument, rather than before like * @@ -462,7 +489,6 @@ class cpp_function : public function { pybind11_fail("Internal error while parsing type signature (2)"); } - signature.erase(std::remove(signature.begin(), signature.end(), '\n'), signature.end()); rec->signature = guarded_strdup(signature.c_str()); rec->args.shrink_to_fit(); rec->nargs = (std::uint16_t) args; From 68b98ef1e84e7d9155b2450d03d89eec60179894 Mon Sep 17 00:00:00 2001 From: JeanElsner Date: Sat, 8 Jul 2023 21:04:18 +0200 Subject: [PATCH 2/2] Fix call to replaceNewlinesAndSquash --- include/pybind11/pybind11.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 0c04d33b82..84d668f001 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -52,7 +52,7 @@ PYBIND11_WARNING_DISABLE_MSVC(4127) PYBIND11_NAMESPACE_BEGIN(detail) -std::string replaceNewlinesAndSquash(const char* text) { +inline std::string replaceNewlinesAndSquash(const char* text) { std::string result; // Replace newlines with spaces and squash consecutive spaces @@ -451,7 +451,7 @@ class cpp_function : public function { // Write default value if available. if (!is_starred && arg_index < rec->args.size() && rec->args[arg_index].descr) { signature += " = "; - signature += replaceNewlinesAndSquash(rec->args[arg_index].descr); + signature += detail::replaceNewlinesAndSquash(rec->args[arg_index].descr); } // Separator for positional-only arguments (placed after the // argument, rather than before like *