std::VSPRINTF 的字符串感知选项
std::string aware options for vsprintf
我有一个旧的 C(>80k 行)的 MUD 代码库,它使用 printf 风格的字符串格式。 它是普遍的 - 几乎每个文本都通过对sprintf或vsprintf包装器的调用运行。 但是,我最近转向使用 g++ 进行编译以利用 STL,并希望在有意义的地方使用 std::string(实际上是默认不区分大小写的比较的派生类)。
显然,您不能将 std::string 作为可变参数之一传递给任何 printf 函数:在任何情况下我都需要 .c_str()。 我不想这样做,主要是因为我不想修改对printf函数的2000+调用。 我的问题是:如何制作一个 std::string aware vsprintf?
在我看来,我有两个选择:编写我自己的printf函数,在传递给std::vsprintf之前迭代参数,将指向std::string的指针更改为std::string.data(或c_out()),或者我可以借用printf的胆量并滚动我自己的。 显然,第一种选择听起来工作量更少。
当然,更好的选择是如果有人以前做过,但我的谷歌搜索一无所获。 关于最佳选择的任何提示?
编辑: 这个问题被关闭为"如何使用C++std::ostream与类似printf的格式?"的副本,我认为它不能回答这个问题。 我不是在问如何使用 std::ostream 与旧的 C printf 输出字符串。 我正在寻求有关旧 C 代码库的补丁解决方案的帮助,该代码库广泛使用 sprintf/vsprintf,而无需重写对这些函数的数千次调用以使用输出流。
您可以制作自己的printf
包装器,从std::string
中提取char const*
。 例如:
#include <iostream>
#include <string>
#include <cstdio>
template<class T>
inline auto to_c(T&& arg) -> decltype(std::forward<T>(arg)) {
return std::forward<T>(arg);
}
inline char const* to_c(std::string const& s) { return s.c_str(); }
inline char const* to_c(std::string& s) { return s.c_str(); }
template<class... Args>
int my_printf(char const* fmt, Args&&... args) {
return std::printf(fmt, to_c(args)...);
}
int main() {
std::string name = "World";
my_printf("Hello, %s!n", name);
}
或者,更好的是,切换到现代C++格式库,例如fmt
.
常见的建议是 Boost.Format
以他们为例:
// printf directives's type-flag can be used to pass formatting options :
std::cout << format("_%1$4d_ is : _%1$#4x_, _%1$#4o_, and _%1$s_ by defaultn") % 18;
// prints "_ 18_ is : _0x12_, _ 022_, and _18_ by defaultn"
现在这假设std::ostream&
,因此您需要一个std::stringstream
才能使用std::string
作为支持缓冲区。
使用派生类进行不区分大小写的比较听起来像是一个等待咬你的坏主意。您只需要一个自定义订单;所有假定排序的 STL 函数都有重载以支持自定义排序。
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- Win32编译器选项和内存分配
- C/C++预处理器是否可以检测一些编译器选项
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- 通过选项卡的文本设置QTabWidget顺序
- 通过ccmake在cmake中缓存依赖选项
- 如何传递多个 std::文件系统选项?
- 基于编译器选项的编译二进制路径
- "perf_event_attr"结构的"read_format"属性的选项到底是什么?
- 如何应用 libcurl 的持久连接选项
- 文件中.dat Dlib 选项
- LLVM | codegen 用于带有命令行选项的程序输入功能
- 编译 Boost 时在 OS X 上的"ld:未知选项:-soname"
- 按钮悬停在 QT 中垂直布局的选项卡小部件中不起作用
- Visual Studio C++ 它只构建选项卡中显示的文件吗?
- 在C++不适用于猜数字游戏的情况下再次播放选项
- Qt5 用户界面编译器:-i 选项不可用
- CMake - 更改共享库链接选项
- std::VSPRINTF 的字符串感知选项