sources/mystring.cpp

Wed, 20 Jul 2016 15:06:00 +0300

author
Teemu Piippo <teemu@compsta2.com>
date
Wed, 20 Jul 2016 15:06:00 +0300
changeset 142
b4f89893c702
parent 137
485cb6d6b98c
child 141
d9073c13dc98
child 145
d0aedc9be448
permissions
-rw-r--r--

Restyled parentheses in coloredline.cpp/.h

/*
	Copyright 2014 - 2016 Teemu Piippo
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions
	are met:

	1. Redistributions of source code must retain the above copyright
	   notice, this list of conditions and the following disclaimer.
	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.
	3. Neither the name of the copyright holder nor the names of its
	   contributors may be used to endorse or promote products derived from
	   this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
	TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
	PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
	OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
	PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
	PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
	LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
	NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <cstring>
#include "main.h"
#include "mystring.h"
#include "md5.h"

BEGIN_ZFC_NAMESPACE

// -------------------------------------------------------------------------------------------------
//
int String::compare (const String& other) const
{
	return m_string.compare (other.std_string());
}

// -------------------------------------------------------------------------------------------------
//
void String::trim (int n)
{
	if (n > 0)
		m_string = mid (0, length() - n).std_string();
	else
		m_string = mid (n, -1).std_string();
}

// -------------------------------------------------------------------------------------------------
//
String String::strip (char unwanted) const
{
	String result (m_string);

	for (int pos = 0; (pos = result.find (unwanted)) != -1;)
		result.remove_at (pos--);

	return result;
}

// -------------------------------------------------------------------------------------------------
//
String String::strip (const List<char>& unwanted) const
{
	String result (m_string);

	for (String c : unwanted)
	for (int pos = 0; (pos = result.find (c)) != -1;)
		result.remove_at (pos--);

	return result;
}

// -------------------------------------------------------------------------------------------------
//
String String::to_uppercase() const
{
	String result (m_string);

	for (char &ch : result)
	{
		if (islower(ch))
			ch -= 'a' - 'A';
	}

	return result;
}

// -------------------------------------------------------------------------------------------------
//
String String::to_lowercase() const
{
	String result (m_string);

	for (char &ch : result)
	{
		if (isupper(ch))
			ch += 'a' - 'A';
	}

	return result;
}

// -------------------------------------------------------------------------------------------------
//
StringList String::split (char del) const
{
	String delimstr;
	delimstr += del;
	return split (delimstr);
}

// -------------------------------------------------------------------------------------------------
//
StringList String::split (const String& del) const
{
	StringList res;
	int a = 0;
	int b;

	// Find all separators and store the text left to them.
	while ((b = find (del, a)) != -1)
	{
		String sub = mid (a, b);

		if (sub.length() > 0)
			res << sub;

		a = b + del.length();
	}

	// Add the string at the right of the last separator
	if (a < (int) length())
		res.append (mid (a, length()));

	return res;
}

// -------------------------------------------------------------------------------------------------
//
void String::replace (const char* a, const char* b)
{
	long pos;

	while ((pos = find (a)) != -1)
		m_string = m_string.replace (pos, strlen (a), b);
}

// -------------------------------------------------------------------------------------------------
//
int String::count (char needle) const
{
	int result = 0;

	for (char ch : *this)
	{
		if (ch == needle)
			result++;
	}

	return result;
}

// -------------------------------------------------------------------------------------------------
//
// Returns a substring from [a, b)
//
String String::mid (int a, int b) const
{
	a = max(a, 0);
	b = min(b, length());

	if (b == -1)
		b = length();

	if (b <= a)
		return "";
	else
		return m_string.substr(a, b - a);
}

// -------------------------------------------------------------------------------------------------
//
String String::right(int length) const
{
	if (length >= this->length())
		return *this;
	else
		return String(chars() + this->length() - length);
}

// -------------------------------------------------------------------------------------------------
//
int String::word_position (int n) const
{
	int count = 0;

	for (char ch : *this)
	{
		if (not isspace(ch) or ++count < n)
			continue;
	}

	return -1;
}

// -------------------------------------------------------------------------------------------------
//
int String::find (const char* c, int a) const
{
	int pos = m_string.find (c, a);

	if (pos == int (std::string::npos))
		return -1;

	return pos;
}

// -------------------------------------------------------------------------------------------------
//
int String::find (char ch, int a) const
{
	int pos = m_string.find (ch, a);

	if (pos == int (std::string::npos))
		return -1;

	return pos;
}

// -------------------------------------------------------------------------------------------------
//
int String::find_last (const char* c, int a) const
{
	if (a == -1 or a >= length())
		a = length() - 1;

	for (; a > 0; a--)
		if (m_string[a] == c[0] and strncmp (chars() + a, c, strlen (c)) == 0)
			return a;

	return -1;
}

// -------------------------------------------------------------------------------------------------
//
long String::to_int (bool* ok, int base) const
{
	errno = 0;
	char* endptr;
	long i = strtol (chars(), &endptr, base);

	if (ok)
		*ok = (errno == 0 and *endptr == '\0');

	return i;
}

// -------------------------------------------------------------------------------------------------
//
float String::to_float (bool* ok) const
{
	errno = 0;
	char* endptr;
	float i = (float) strtod (chars(), &endptr);

	if (ok != nullptr)
		*ok = (errno == 0 and *endptr == '\0');

	return i;
}

// -------------------------------------------------------------------------------------------------
//
double String::to_double (bool* ok) const
{
	errno = 0;
	char* endptr;
	double i = strtod (chars(), &endptr);

	if (ok != nullptr)
		*ok = (errno == 0 and *endptr == '\0');

	return i;
}

// -------------------------------------------------------------------------------------------------
//
String String::operator+ (const String& data) const
{
	String newString = *this;
	newString.append (data);
	return newString;
}

// -------------------------------------------------------------------------------------------------
//
String String::operator+ (const char* data) const
{
	String newstr = *this;
	newstr.append (data);
	return newstr;
}

// -------------------------------------------------------------------------------------------------
//
bool String::is_numeric() const
{
	char* endptr;
	strtol (chars(), &endptr, 10);
	return not (endptr && *endptr);
}

// -------------------------------------------------------------------------------------------------
//
bool String::ends_with (const String& other)
{
	if (length() < other.length())
		return false;

	const int ofs = length() - other.length();
	return strncmp (chars() + ofs, other.chars(), other.length()) == 0;
}

// -------------------------------------------------------------------------------------------------
//
bool String::starts_with (const String& other) const
{
	if (length() < other.length())
		return false;

	return strncmp (chars(), other.chars(), other.length()) == 0;
}

// -------------------------------------------------------------------------------------------------
//
void __cdecl String::sprintf (const char* fmtstr, ...)
{
	va_list args;
	va_start (args, fmtstr);
	this->vsprintf (fmtstr, args);
	va_end (args);
}

// -------------------------------------------------------------------------------------------------
//
void String::vsprintf (const char* fmtstr, va_list args)
{
	char* buf = nullptr;
	int bufsize = 256;

	do
	{
		bufsize *= 2;
		delete[] buf;
		buf = new char[bufsize];
	}
	while (vsnprintf (buf, bufsize, fmtstr, args) >= bufsize);

	m_string = buf;
	delete[] buf;
}

// -------------------------------------------------------------------------------------------------
//
String StringList::join (const String& delim)
{
	String result;

	for (const String &item : container())
	{
		if (result.is_empty() == false)
			result += delim;

		result += item;
	}

	return result;
}

// -------------------------------------------------------------------------------------------------
//
bool String::mask_against (const String& pattern) const
{
	// Elevate to uppercase for case-insensitive matching
	String pattern_upper = pattern.to_uppercase();
	String this_upper = to_uppercase();
	const char* maskstring = pattern_upper.chars();
	const char* mptr = &maskstring[0];

	for (const char* sptr = this_upper.chars(); *sptr != '\0'; sptr++)
	{
		if (*mptr == '?')
		{
			if (*(sptr + 1) == '\0')
			{
				// ? demands that there's a character here and there wasn't.
				// Therefore, mask matching fails
				return false;
			}
		}
		else if (*mptr == '*')
		{
			char end = *(++mptr);

			// If '*' is the final character of the message, all of the remaining
			// string matches against the '*'. We don't need to bother checking
			// the string any further.
			if (end == '\0')
				return true;

			// Skip to the end character
			while (*sptr != end and *sptr != '\0')
				sptr++;

			// String ended while the mask still had stuff
			if (*sptr == '\0')
				return false;
		}
		else if (*sptr != *mptr)
			return false;

		mptr++;
	}

	return true;
}

// -------------------------------------------------------------------------------------------------
//
String String::from_number (short int a)
{
	char buf[32];
	::sprintf (buf, "%d", a);
	return String (buf);
}

// -------------------------------------------------------------------------------------------------
//
String String::from_number (int a)
{
	char buf[32];
	::sprintf (buf, "%d", a);
	return String (buf);
}

// -------------------------------------------------------------------------------------------------
//
String String::from_number (long int a)
{
	char buf[32];
	::sprintf (buf, "%ld", a);
	return String (buf);
}

// -------------------------------------------------------------------------------------------------
//
String String::from_number (unsigned short int a)
{
	char buf[32];
	::sprintf (buf, "%u", a);
	return String (buf);
}

// -------------------------------------------------------------------------------------------------
//
String String::from_number (unsigned int a)
{
	char buf[32];
	::sprintf (buf, "%u", a);
	return String (buf);
}

// -------------------------------------------------------------------------------------------------
//
String String::from_number (unsigned long int a)
{
	char buf[32];
	::sprintf (buf, "%lu", a);
	return String (buf);
}

// -------------------------------------------------------------------------------------------------
//
String String::from_number (double a)
{
	char buf[64];
	::sprintf (buf, "%f", a);
	return String (buf);
}

// -------------------------------------------------------------------------------------------------
//
String String::md5() const
{
	char checksum[33];
	CalculateMD5 (reinterpret_cast<const unsigned char*> (chars()), length(), checksum);
	checksum[sizeof checksum - 1] = '\0';
	return String (checksum);
}

// -------------------------------------------------------------------------------------------------
//
void String::normalize (int (*filter)(int))
{
	int a = 0;
	int b = length() - 1;

	while ((*filter) (m_string[a]) and a != b)
		++a;

	while ((*filter) (m_string[b]) and a != b)
		--b;

	if (a == b)
		m_string = "";
	else if (a != 0 or b != length() - 1)
		m_string = m_string.substr (a, b - a + 1);
}

// -------------------------------------------------------------------------------------------------
//
String String::normalized (int (*filter)(int)) const
{
	String result = *this;
	result.normalize(filter);
	return result;
}

END_ZFC_NAMESPACE

mercurial