moo::format
moo::format is a light "std::format / fmt" substitute.
Description:
moo::format is a light substitute to "std::format/fmt". It inserts arguments into a string model, example code can be found below.
Side Quest: If you are looking to make a little bit of coin the moo::formatParse function could use an upgrade.
Parameters:
moo::format(const char* formatString, Args... args);
​
| Name |
Type |
Description |
| formatString |
const char* |
The string "model/template" that the additional arguments are inserted into. |
| args |
std::string
const char*
char
int
long
long long
unsigned
unsigned long
unsigned long long
float
double
long double
|
The values inserted into the the string "model/template". |
Source Code:
#pragma once
#include <string>
namespace moo
{
	inline std::string to_string(std::string arg) { return arg; }
	inline std::string to_string(const char* arg) { return std::string(arg); }
	inline std::string to_string(char arg) { return std::string(1, arg); }
	inline std::string to_string(int arg) { return std::to_string(arg); }
	inline std::string to_string(long arg) { return std::to_string(arg); }
	inline std::string to_string(long long arg) { return std::to_string(arg); }
	inline std::string to_string(unsigned int arg) { return std::to_string(arg); }
	inline std::string to_string(unsigned long arg) { return std::to_string(arg); }
	inline std::string to_string(unsigned long long arg) { return std::to_string(arg); }
	inline std::string to_string(float arg) { return std::to_string(arg); }
	inline std::string to_string(double arg) { return std::to_string(arg); }
	inline std::string to_string(long double arg) { return std::to_string(arg); }
	
	
	
	
	
	
	
	
	
	
	
	
	inline std::string formatParse(const char* formatString, std::string value)
	{
		std::string retStr("");
		for (int index = 0; formatString[index + 1] != '\0' ; ++index)
		{
			if (formatString[index] == '{' && formatString[index + 1] == '}')
			{
				retStr += value;
				if (formatString[index + 2] != '\0')
				{
					retStr += &(formatString[index + 2]);
				}
				return retStr;
			}
			retStr += formatString[index];
		}
		return retStr;
	}
	template<typename T>
	inline std::string format(const char* formatString, T t)
	{
		return formatParse(formatString, to_string(t));
	}
	template<typename T, typename... Args>
	inline std::string format(const char* formatString, T t, Args... args)
	{
		return format(formatParse(formatString, to_string(t)).c_str(), args...);
	}
}
Example:
#include <iostream>
#include "format.h"
int main()
{
	std::cout << moo::format("{} are ${}", "chocolate bars", 1.25);
	return 0;
}
Setup Guide:
- Download or copy the format.h file.
- Add the header file to your project.
- Add your include directive... #include "{filepath}/format.h".
- (Optional) Run the unit tests, found at the bottom of the format.h file.
- Profit?