summaryrefslogtreecommitdiff
path: root/src/util.h
diff options
context:
space:
mode:
authorLibravatar Mubashshir <ahmubashshir@gmail.com>2024-11-16 00:32:45 +0600
committerLibravatar Mubashshir <ahmubashshir@gmail.com>2024-11-16 00:32:45 +0600
commit47d602cf884b14e4c529286238e2085fe97338d7 (patch)
tree8d7bb77e1dac06f96845ff6c4d092f3a8d4cb0c3 /src/util.h
parent1f0ff6b0675f514b394c2c6fd44e4ce32b84c7b0 (diff)
downloadlog-parser-47d602cf884b14e4c529286238e2085fe97338d7.tar.gz
log-parser-47d602cf884b14e4c529286238e2085fe97338d7.zip
Add util string formatterHEADmaster
Signed-off-by: Mubashshir <ahmubashshir@gmail.com>
Diffstat (limited to 'src/util.h')
-rw-r--r--src/util.h19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/util.h b/src/util.h
new file mode 100644
index 0000000..a54d3eb
--- /dev/null
+++ b/src/util.h
@@ -0,0 +1,19 @@
+#ifndef __UTIL_H__
+#define __UTIL_H__
+
+#include <memory>
+#include <string>
+#include <stdexcept>
+
+template<typename ... Args>
+std::string string_format( const std::string& format, Args ... args )
+{
+ int size_s = std::snprintf( nullptr, 0, format.c_str(), args ... ) + 1; // Extra space for '\0'
+ if( size_s <= 0 ){ throw std::runtime_error( "Error during formatting." ); }
+ auto size = static_cast<size_t>( size_s );
+ std::unique_ptr<char[]> buf( new char[ size ] );
+ std::snprintf( buf.get(), size, format.c_str(), args ... );
+ return std::string( buf.get(), buf.get() + size - 1 ); // We don't want the '\0' inside
+}
+
+#endif /* __UTIL_H__ */