I am trying to implement the same function as C#'s String.Format in C++. However, I have a problem with the output in some cases:
#include <iostream>
#include <string>
class format_base
{
public:
format_base(const char *base) : argp_(0)
{
base_ = (char*)calloc(4097, sizeof(char));
sz_ = 4097;
memcpy(base_, base, strlen(base));
};
~format_base()
{
free(base_);
}
format_base &arg(const char *argument)
{
if ((strlen(base_) - 3) + strlen(argument) > sz_)
{
base_ = (char*)realloc(base_, (sz_ *= 2));
}
std::string elim = "{";
elim.append(std::to_string(argp_++));
elim.append("}");
std::string tbase = base_;
size_t f = tbase.find(elim.c_str());
if (f != std::string::npos)
{
tbase.replace(f, elim.size(), argument);
}
memcpy(base_, tbase.c_str(), tbase.length());
return *this;
}
char *value()
{
return base_;
}
char *operator()()
{
return base_;
}
private:
char *base_;
size_t argp_, sz_;
};
format_base &format(const char *base)
{
return *new format_base(base);
}
int main()
{
std::cout << format("Hello {0}").arg("a")(); // Prints "Hello a0} "
std::cout << format(" Hello {0}").arg("ab")(); // Prints " Hello ab} "
std::cout << format(" Hello {0}\n").arg("abc")(); // Prints " Hello abc\n"
std::cout << format("Hello {0}\n").arg("a")(); // Prints "Hello a\n}"
std::cout << format("Hello {0}\n").arg("ab")(); // Prints "Hello ab\n"
std::cout << format("Hello {0}\n").arg("abc")(); // Prints "Hello abc\n"
getchar();
}
Total output:
Hello a0} Hello ab} Hello abc
Hello a
}
Hello ab
_
Hello abc
_
I am very confused and I would be very grateful if you could help
std::to_stringfunction that returns a properstd::string. No need to make another that returns a C string.