mirror of
https://github.com/znc/znc.git
synced 2026-03-28 17:42:41 +01:00
Added CString::Join and CString::Convert
CString::Join works like python's string join, except that it takes 2 iterators (or pointers) instead of a whole collection CString::Convert casts a string to another type using an intermediary stringstream.
This commit is contained in:
committed by
Alexey Sokolov
parent
be6094a464
commit
adafe9d197
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2004-2014 ZNC, see the NOTICE file for details.
|
||||
* Copyright (C) 2004-2013 ZNC, see the NOTICE file for details.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -22,6 +22,7 @@
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
#include <sys/types.h>
|
||||
|
||||
#define _SQL(s) CString("'" + CString(s).Escape_n(CString::ESQL) + "'")
|
||||
@@ -94,7 +95,41 @@ public:
|
||||
CString(const std::string& s) : std::string(s) {}
|
||||
CString(size_t n, char c) : std::string(n, c) {}
|
||||
~CString() {}
|
||||
|
||||
|
||||
/**
|
||||
* Casts a CString to another type. Implemented via std::stringstream, you use this
|
||||
* for any class that has an operator<<(std::ostream, YourClass).
|
||||
* @param target The object to cast into. If the cast fails, its state is unspecified.
|
||||
* @return True if the cast succeeds, and false if it fails.
|
||||
*/
|
||||
template <typename T> bool Convert(T *target) const
|
||||
{
|
||||
std::stringstream ss(*this);
|
||||
ss >> *target;
|
||||
return (bool) ss; // we don't care why it failed, only whether it failed
|
||||
}
|
||||
|
||||
/**
|
||||
* Joins a collection of objects together, using 'this' as a delimiter.
|
||||
* You can pass either pointers to arrays, or iterators to collections.
|
||||
* @param i_begin An iterator pointing to the beginning of a group of objects.
|
||||
* @param i_end An iterator pointing past the end of a group of objects.
|
||||
* @return The joined string
|
||||
*/
|
||||
template <typename Iterator> CString Join(Iterator i_start, const Iterator &i_end) const
|
||||
{
|
||||
if (i_start == i_end) return CString("");
|
||||
std::ostringstream output;
|
||||
output << *i_start;
|
||||
while (true)
|
||||
{
|
||||
++i_start;
|
||||
if (i_start == i_end) return CString(output.str());
|
||||
output << *this;
|
||||
output << *i_start;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare this string caselessly to some other string.
|
||||
* @param s The string to compare to.
|
||||
|
||||
Reference in New Issue
Block a user