Sha256: 26d80f907574182cbf39b5d6db80e83663b00ce2f94d8224f06e69abd914fa4e

Contents?: true

Size: 1.48 KB

Versions: 10

Compression:

Stored size: 1.48 KB

Contents

#pragma once

#include <vector>
#include <string>
#include <iostream>
#include <iterator>
#include <iomanip>
#include <algorithm>
#include <cstdio>

namespace tomoto
{
	namespace text
	{
		template<typename ... _Args>
		std::string format(const std::string& format, _Args ... args)
		{
			size_t size = snprintf(nullptr, 0, format.c_str(), args ...) + 1;
			std::vector<char> buf(size);
			snprintf(buf.data(), size, format.c_str(), args ...);
			return std::string{ buf.data(), buf.data() + size - 1 };
		}

		template<class _Iter, class _Target = decltype(*_Iter{}) >
		std::string join(_Iter first, _Iter last, const std::string& delimiter = ",")
		{
			if (first == last) return "";
			std::ostringstream stream;
			std::copy(first, last, std::ostream_iterator<_Target>(stream, delimiter.c_str()));
			std::string s = stream.str();
			s.erase(s.end() - delimiter.size(), s.end());
			return s;
		}

		inline std::string quote(const std::string& s)
		{
			std::ostringstream stream;
			stream << std::quoted(s);
			return stream.str();
		}

		inline std::vector<std::string> split(const std::string& str, const std::string& delim)
		{
			std::vector<std::string> tokens;
			size_t prev = 0, pos = 0;
			do
			{
				pos = str.find(delim, prev);
				if (pos == std::string::npos) pos = str.length();
				std::string token = str.substr(prev, pos - prev);
				if (!token.empty()) tokens.push_back(token);
				prev = pos + delim.length();
			} while (pos < str.length() && prev < str.length());
			return tokens;
		}
	}
}

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
tomoto-0.4.1 vendor/tomotopy/src/Utils/text.hpp
tomoto-0.4.0 vendor/tomotopy/src/Utils/text.hpp
tomoto-0.3.3 vendor/tomotopy/src/Utils/text.hpp
tomoto-0.3.2 vendor/tomotopy/src/Utils/text.hpp
tomoto-0.3.1 vendor/tomotopy/src/Utils/text.hpp
tomoto-0.3.0 vendor/tomotopy/src/Utils/text.hpp
tomoto-0.2.3 vendor/tomotopy/src/Utils/text.hpp
tomoto-0.2.2 vendor/tomotopy/src/Utils/text.hpp
tomoto-0.2.1 vendor/tomotopy/src/Utils/text.hpp
tomoto-0.2.0 vendor/tomotopy/src/Utils/text.hpp