finalcut/src/fstring.cpp

1215 lines
28 KiB
C++
Raw Normal View History

2017-11-04 07:03:53 +01:00
/***********************************************************************
* fstring.cpp - Unicode string class with UTF-8 support *
* *
* This file is part of the FINAL CUT widget toolkit *
2017-11-04 07:03:53 +01:00
* *
* Copyright 2012-2021 Markus Gans *
2017-11-04 07:03:53 +01:00
* *
* FINAL CUT is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of *
2017-11-04 07:03:53 +01:00
* the License, or (at your option) any later version. *
* *
* FINAL CUT is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
2017-11-04 07:03:53 +01:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program. If not, see *
* <http://www.gnu.org/licenses/>. *
***********************************************************************/
2015-05-23 13:35:12 +02:00
#include <algorithm>
2017-09-11 03:06:02 +02:00
#include <string>
2019-08-06 23:45:28 +02:00
#include <utility>
2017-09-11 03:06:02 +02:00
#include <vector>
2020-05-13 23:47:14 +02:00
#include "final/fapplication.h"
#include "final/flog.h"
#include "final/fstring.h"
2015-05-23 13:35:12 +02:00
namespace finalcut
{
2019-09-29 22:28:58 +02:00
// static class attributes
wchar_t FString::null_char{L'\0'};
const wchar_t FString::const_null_char{L'\0'};
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
// class FString
//----------------------------------------------------------------------
// constructors and destructor
//----------------------------------------------------------------------
FString::FString (int len)
{
if ( len > 0 )
string = std::wstring(std::size_t(len), L'\0');
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
FString::FString (std::size_t len)
2021-06-03 16:08:36 +02:00
: string{std::wstring(len, L'\0')}
{ }
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
FString::FString (std::size_t len, wchar_t c)
2021-06-03 16:08:36 +02:00
: string{std::wstring(len, c)}
{ }
2015-05-23 13:35:12 +02:00
2020-12-31 20:45:10 +01:00
//----------------------------------------------------------------------
FString::FString (std::size_t len, const UniChar& c)
: FString(len, wchar_t(c))
{ }
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
FString::FString (const FString& s) // copy constructor
{
if ( ! s.isEmpty() )
2021-06-03 16:08:36 +02:00
{
std::wstring copy(s.string);
_assign (copy);
}
}
2015-05-23 13:35:12 +02:00
2019-07-29 02:34:58 +02:00
//----------------------------------------------------------------------
2020-04-13 12:40:11 +02:00
FString::FString (FString&& s) noexcept // move constructor
: string{s.string}
2019-07-29 02:34:58 +02:00
{
s.string.clear();
2019-07-29 02:34:58 +02:00
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
FString::FString (const std::wstring& s)
{
if ( ! s.empty() )
2021-06-03 16:08:36 +02:00
{
std::wstring str(s);
_assign (str);
}
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
FString::FString (const wchar_t s[])
2015-05-23 13:35:12 +02:00
{
if ( s )
2021-06-03 16:08:36 +02:00
{
std::wstring str(s);
_assign (str);
}
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
FString::FString (const std::string& s)
{
if ( ! s.empty() )
2018-03-05 03:15:16 +01:00
{
2021-06-03 16:08:36 +02:00
auto wide_string = _toWideString(s.c_str());
_assign(wide_string);
2018-03-05 03:15:16 +01:00
}
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
FString::FString (const char s[])
2015-05-23 13:35:12 +02:00
{
if ( s )
2018-03-05 03:15:16 +01:00
{
2021-06-03 16:08:36 +02:00
auto wide_string = _toWideString(s);
_assign(wide_string);
2018-03-05 03:15:16 +01:00
}
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
2020-12-31 20:45:10 +01:00
FString::FString (const UniChar& c)
{
2021-06-03 16:08:36 +02:00
std::wstring str{ static_cast<wchar_t>(c) };
_assign (str);
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
FString::FString (const wchar_t c)
{
if ( c )
2018-03-05 03:15:16 +01:00
{
2021-06-03 16:08:36 +02:00
std::wstring str{ c };
_assign (str);
2018-03-05 03:15:16 +01:00
}
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
FString::FString (const char c)
{
if ( c )
2018-03-05 03:15:16 +01:00
{
2021-06-03 16:08:36 +02:00
std::wstring str{ wchar_t(c & 0xff) };
_assign (str);
2018-03-05 03:15:16 +01:00
}
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
2021-06-03 16:08:36 +02:00
FString::~FString() = default; // destructor
2015-05-23 13:35:12 +02:00
// FString operators
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
FString& FString::operator = (const FString& s)
2015-05-23 13:35:12 +02:00
{
if ( &s != this )
2021-06-03 16:08:36 +02:00
{
std::wstring str{s.string};
_assign (str);
}
return *this;
2015-05-23 13:35:12 +02:00
}
2019-07-29 02:34:58 +02:00
//----------------------------------------------------------------------
2020-04-13 12:40:11 +02:00
FString& FString::operator = (FString&& s) noexcept
2019-07-29 02:34:58 +02:00
{
if ( &s != this )
{
string = s.string;
s.string.clear();
}
2020-05-24 02:15:43 +02:00
2019-07-29 02:34:58 +02:00
return *this;
}
//----------------------------------------------------------------------
const FString& FString::operator += (const FString& s)
{
string.append(s.string);
return *this;
}
2017-09-20 02:51:17 +02:00
//----------------------------------------------------------------------
FString& FString::operator << (const FString& s)
{
string.append(s.string);
2017-09-20 02:51:17 +02:00
return *this;
}
//----------------------------------------------------------------------
2020-12-31 20:45:10 +01:00
FString& FString::operator << (const UniChar& c)
{
FString s{static_cast<wchar_t>(c)};
string.append(s.string);
return *this;
}
2017-09-20 02:51:17 +02:00
//----------------------------------------------------------------------
FString& FString::operator << (const wchar_t c)
{
FString s{c};
string.append(s.string);
2017-09-20 02:51:17 +02:00
return *this;
}
//----------------------------------------------------------------------
FString& FString::operator << (const char c)
{
FString s{c};
string.append(s.string);
2017-09-20 02:51:17 +02:00
return *this;
}
//----------------------------------------------------------------------
2020-07-12 15:25:21 +02:00
const FString& FString::operator >> (FString& s) const
2017-09-20 02:51:17 +02:00
{
s.string.append(string);
2017-09-20 02:51:17 +02:00
return *this;
}
//----------------------------------------------------------------------
2020-07-12 15:25:21 +02:00
const FString& FString::operator >> (std::wstring& s) const
2017-09-20 02:51:17 +02:00
{
s += string;
2017-09-20 02:51:17 +02:00
return *this;
}
2018-03-03 22:24:57 +01:00
//----------------------------------------------------------------------
2020-07-12 15:25:21 +02:00
const FString& FString::operator >> (std::string& s) const
2018-03-03 22:24:57 +01:00
{
s.append(toString());
2018-03-03 22:24:57 +01:00
return *this;
}
2017-09-20 02:51:17 +02:00
//----------------------------------------------------------------------
2020-07-12 15:25:21 +02:00
const FString& FString::operator >> (wchar_t& c) const
2017-09-20 02:51:17 +02:00
{
c = ( ! isEmpty() ) ? string[0] : L'\0';
2017-09-20 02:51:17 +02:00
return *this;
}
//----------------------------------------------------------------------
2020-07-12 15:25:21 +02:00
const FString& FString::operator >> (char& c) const
2017-09-20 02:51:17 +02:00
{
c = ( ! isEmpty() ) ? char(string[0] & 0xff) : '\0';
2017-09-20 02:51:17 +02:00
return *this;
}
//----------------------------------------------------------------------
2020-07-12 15:25:21 +02:00
const FString& FString::operator >> (sInt16& num) const
2017-09-20 02:51:17 +02:00
{
num = toShort();
return *this;
}
//----------------------------------------------------------------------
2020-07-12 15:25:21 +02:00
const FString& FString::operator >> (uInt16& num) const
2017-09-20 02:51:17 +02:00
{
num = toUShort();
return *this;
}
//----------------------------------------------------------------------
2020-07-12 15:25:21 +02:00
const FString& FString::operator >> (sInt32& num) const
2017-09-20 02:51:17 +02:00
{
num = toInt();
return *this;
}
//----------------------------------------------------------------------
2020-07-12 15:25:21 +02:00
const FString& FString::operator >> (uInt32& num) const
2017-09-20 02:51:17 +02:00
{
num = toUInt();
return *this;
}
//----------------------------------------------------------------------
2020-07-12 15:25:21 +02:00
const FString& FString::operator >> (sInt64& num) const
2017-09-20 02:51:17 +02:00
{
num = toLong();
return *this;
}
//----------------------------------------------------------------------
2020-07-12 15:25:21 +02:00
const FString& FString::operator >> (uInt64& num) const
2017-09-20 02:51:17 +02:00
{
num = toULong();
return *this;
}
//----------------------------------------------------------------------
2020-07-12 15:25:21 +02:00
const FString& FString::operator >> (double& num) const
2017-09-20 02:51:17 +02:00
{
num = toDouble();
return *this;
}
//----------------------------------------------------------------------
2020-07-12 15:25:21 +02:00
const FString& FString::operator >> (float& num) const
2017-09-20 02:51:17 +02:00
{
num = toFloat();
return *this;
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
2020-07-12 15:25:21 +02:00
FString::operator bool () const
{
return ! isEmpty();
2020-07-12 15:25:21 +02:00
}
//----------------------------------------------------------------------
const FString& FString::operator () () const
2015-05-23 13:35:12 +02:00
{
return *this;
2015-05-23 13:35:12 +02:00
}
// public methods of FString
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
FString FString::clear()
{
string.clear();
return *this;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
const wchar_t* FString::wc_str() const
{
// Returns a constant wide character string
return string.c_str();
}
//----------------------------------------------------------------------
wchar_t* FString::wc_str()
{
// Returns a wide character string
return const_cast<wchar_t*>(string.c_str());
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
const char* FString::c_str() const
{
// Returns a constant c-string
if ( ! isEmpty() )
{
char_string = _toCharString(string);
return char_string.c_str();
}
else
return "";
}
//----------------------------------------------------------------------
char* FString::c_str()
{
// Returns a c-string
if ( ! isEmpty() )
2020-10-14 17:31:52 +02:00
{
char_string = _toCharString(string);
return const_cast<char*>(char_string.c_str());
2020-10-14 17:31:52 +02:00
}
else
return const_cast<char*>("");
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
std::string FString::toString() const
2015-05-23 13:35:12 +02:00
{
return c_str();
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
FString FString::toLower() const
2015-05-23 13:35:12 +02:00
{
FString s{*this};
auto to_lower = [] (wchar_t& c)
{
c = wchar_t(std::towlower(std::wint_t(c)));
};
std::for_each (s.begin(), s.end(), to_lower);
2015-05-23 13:35:12 +02:00
return s;
}
//----------------------------------------------------------------------
FString FString::toUpper() const
2015-05-23 13:35:12 +02:00
{
FString s{*this};
auto to_upper = [] (wchar_t& c)
{
c = wchar_t(std::towupper(std::wint_t(c)));
};
std::for_each (s.begin(), s.end(), to_upper);
2015-05-23 13:35:12 +02:00
return s;
}
//----------------------------------------------------------------------
sInt16 FString::toShort() const
{
const long num = toLong();
2018-03-05 00:25:05 +01:00
if ( num > SHRT_MAX )
throw std::overflow_error ("overflow");
2018-03-05 00:25:05 +01:00
if ( num < SHRT_MIN )
throw std::underflow_error ("underflow");
return static_cast<sInt16>(num);
}
//----------------------------------------------------------------------
uInt16 FString::toUShort() const
{
const uLong num = toULong();
if ( num > USHRT_MAX )
throw std::overflow_error ("overflow");
return static_cast<uInt16>(num);
}
//----------------------------------------------------------------------
int FString::toInt() const
{
const long num = toLong();
2018-03-05 00:25:05 +01:00
if ( num > INT_MAX )
throw std::overflow_error ("overflow");
2018-03-05 00:25:05 +01:00
if ( num < INT_MIN )
throw std::underflow_error ("underflow");
return static_cast<int>(num);
}
//----------------------------------------------------------------------
uInt FString::toUInt() const
{
const uLong num = toULong();
if ( num > UINT_MAX )
throw std::overflow_error ("overflow");
return static_cast<uInt>(num);
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
long FString::toLong() const
{
2019-08-25 22:16:00 +02:00
bool neg{false};
long num{0};
long tenth_limit{LONG_MAX / 10};
long tenth_limit_digit{LONG_MAX % 10};
const FString s{trim()};
const wchar_t* p = s.string.c_str();
2015-05-23 13:35:12 +02:00
if ( ! p )
throw std::invalid_argument ("null value");
if ( ! *p )
throw std::invalid_argument ("empty value");
if ( *p == L'-' )
2015-05-23 13:35:12 +02:00
{
p++;
neg = true;
tenth_limit = -(LONG_MIN / 10);
tenth_limit_digit += 1;
}
else if ( *p == L'+' )
{
p++;
}
while ( std::iswdigit(std::wint_t(*p)) )
{
auto d = uChar(*p - L'0');
if ( num > tenth_limit
2017-11-26 22:37:18 +01:00
|| (num == tenth_limit && d > tenth_limit_digit) )
2015-05-23 13:35:12 +02:00
{
2018-03-05 00:25:05 +01:00
if ( neg )
throw std::underflow_error ("underflow");
else
throw std::overflow_error ("overflow");
2015-05-23 13:35:12 +02:00
}
num = (num << 3) + (num << 1) + d; // (10 * num) + d
p++;
2015-05-23 13:35:12 +02:00
}
if ( *p != L'\0' && ! std::iswdigit(std::wint_t(*p)) )
throw std::invalid_argument ("no valid number");
if ( neg )
num = (~num) + 1;
2021-03-10 10:07:32 +01:00
return num;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
uLong FString::toULong() const
{
2019-08-25 22:16:00 +02:00
uLong num{0};
const uLong tenth_limit{ULONG_MAX / 10};
const uLong tenth_limit_digit{ULONG_MAX % 10};
const FString s{trim()};
const wchar_t* p = s.string.c_str();
2015-05-23 13:35:12 +02:00
if ( ! p )
throw std::invalid_argument ("null value");
if ( ! *p )
throw std::invalid_argument ("empty value");
2018-03-05 00:25:05 +01:00
if ( *p == L'-' )
{
throw std::underflow_error ("underflow");
}
else if ( *p == L'+' )
2015-05-23 13:35:12 +02:00
{
p++;
}
while ( std::iswdigit(std::wint_t(*p)) )
{
const auto d = uChar(*p - L'0');
if ( num > tenth_limit
2017-11-26 22:37:18 +01:00
|| (num == tenth_limit && d > tenth_limit_digit) )
2015-05-23 13:35:12 +02:00
{
throw std::overflow_error ("overflow");
2015-05-23 13:35:12 +02:00
}
num = (num << 3) + (num << 1) + d; // (10 * num) + d
p++;
2015-05-23 13:35:12 +02:00
}
if ( *p != L'\0' && ! std::iswdigit(std::wint_t(*p)) )
throw std::invalid_argument ("no valid number");
2021-03-10 10:07:32 +01:00
return num;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
float FString::toFloat() const
{
const double num = toDouble();
2018-03-05 03:15:16 +01:00
if ( num > double(FLT_MAX) || num < double(-FLT_MAX) )
throw std::overflow_error ("overflow");
if ( std::fabs(num) < double(FLT_EPSILON) ) // num == 0.0f
2018-03-05 00:25:05 +01:00
throw std::underflow_error ("underflow");
return static_cast<float>(num);
}
//----------------------------------------------------------------------
double FString::toDouble() const
{
if ( isEmpty() )
throw std::invalid_argument ("null value");
2019-08-25 22:16:00 +02:00
wchar_t* p{};
const double ret = std::wcstod(string.c_str(), &p);
2020-08-30 22:47:24 +02:00
if ( p != nullptr && *p != L'\0' )
throw std::invalid_argument ("no valid floating point value");
if ( errno == ERANGE )
{
if ( ret >= HUGE_VAL || ret <= -HUGE_VAL )
throw std::overflow_error ("overflow");
2017-09-11 03:06:02 +02:00
if ( std::fabs(ret) < DBL_EPSILON ) // ret == 0.0l
throw std::underflow_error ("underflow");
}
return ret;
}
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
FString FString::ltrim() const
2015-05-23 13:35:12 +02:00
{
// Handle empty string
if ( isEmpty() )
return *this;
auto iter = string.begin();
const auto last = string.end();
while ( iter != last && std::iswspace(std::wint_t(*iter)) )
++iter;
if ( iter != last )
return std::wstring(iter, last);
else
return {};
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
FString FString::rtrim() const
2015-05-23 13:35:12 +02:00
{
// Handle empty string
if ( isEmpty() )
return *this;
const auto first = string.begin();
auto iter = string.end() - 1;
while ( iter != first && std::iswspace(std::wint_t(*iter)) )
--iter;
if ( first != iter )
return std::wstring(first, iter + 1);
2015-05-23 13:35:12 +02:00
else
return {};
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
FString FString::trim() const
2015-05-23 13:35:12 +02:00
{
// Handle NULL and empty string
if ( isEmpty() )
return *this;
const FString s{ltrim()};
2015-05-23 13:35:12 +02:00
return s.rtrim();
}
//----------------------------------------------------------------------
FString FString::left (std::size_t len) const
2015-05-23 13:35:12 +02:00
{
// Handle empty and too long strings
if ( isEmpty() || len > getLength() )
return *this;
return string.substr(0, len);
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
FString FString::right (std::size_t len) const
2015-05-23 13:35:12 +02:00
{
// Handle empty and too long strings
if ( isEmpty() || len > getLength() )
return *this;
return string.substr(string.size() - len, len);
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
FString FString::mid (std::size_t pos, std::size_t len) const
2015-05-23 13:35:12 +02:00
{
// Handle empty string
if ( isEmpty() )
return *this;
2015-05-23 13:35:12 +02:00
if ( pos == 0 )
pos = 1;
auto length = string.length();
if ( pos <= length && pos + len > length )
2015-05-23 13:35:12 +02:00
len = length - pos + 1;
if ( pos > length || pos + len - 1 > length || len == 0 )
return {};
2015-05-23 13:35:12 +02:00
auto first = string.begin() + static_cast<difference_type>(pos) - 1;
auto last = first + static_cast<difference_type>(len);
return std::wstring(first, last);
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
2020-07-12 17:00:16 +02:00
FStringList FString::split (const FString& delimiter) const
2015-05-23 13:35:12 +02:00
{
if ( isEmpty() )
2021-06-03 16:08:36 +02:00
return {};
2015-05-23 13:35:12 +02:00
2021-06-03 16:08:36 +02:00
const FString s{*this};
FStringList string_list{};
auto delimiter_length = delimiter.getLength();
std::wstring::size_type first = 0;
std::wstring::size_type last;
2015-05-23 13:35:12 +02:00
2021-06-03 16:08:36 +02:00
while ( (last = s.string.find(delimiter.string, first)) != std::wstring::npos )
2015-05-23 13:35:12 +02:00
{
2021-06-03 16:08:36 +02:00
string_list.emplace_back(std::wstring(s.string, first, last - first));
first = last + delimiter_length;
2015-05-23 13:35:12 +02:00
}
2021-06-03 16:08:36 +02:00
string_list.emplace_back(std::wstring(s.string, first));
2017-09-20 16:56:20 +02:00
return string_list;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
FString& FString::setString (const FString& s)
2015-05-23 13:35:12 +02:00
{
2021-06-03 16:08:36 +02:00
std::wstring str{s.string};
_assign (str);
return *this;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
FString& FString::setNumber (sInt64 num)
2015-05-23 13:35:12 +02:00
{
2020-10-14 17:31:52 +02:00
std::array<wchar_t, 30> buf{};
2020-04-13 12:40:11 +02:00
wchar_t* s = &buf[29]; // Pointer to the last character
auto abs_num = static_cast<uInt64>(num);
2015-05-23 13:35:12 +02:00
if ( num < 0 )
2019-11-18 16:43:07 +01:00
abs_num = static_cast<uInt64>(-num);
2015-05-23 13:35:12 +02:00
*s = '\0';
2015-05-23 13:35:12 +02:00
do
{
*--s = L"0123456789"[abs_num % 10];
abs_num /= 10;
}
while ( abs_num );
2015-05-23 13:35:12 +02:00
if ( num < 0 )
2015-05-23 13:35:12 +02:00
*--s = '-';
2021-06-03 16:08:36 +02:00
std::wstring str{s};
_assign (str);
2015-05-23 13:35:12 +02:00
return *this;
}
//----------------------------------------------------------------------
FString& FString::setNumber (uInt64 num)
2015-05-23 13:35:12 +02:00
{
wchar_t buf[30]{};
2020-04-13 12:40:11 +02:00
wchar_t* s = &buf[29]; // Pointer to the last character
2015-05-23 13:35:12 +02:00
*s = '\0';
do
{
*--s = L"0123456789"[num % 10];
2015-05-23 13:35:12 +02:00
num /= 10;
}
while ( num );
2015-05-23 13:35:12 +02:00
2021-06-03 16:08:36 +02:00
std::wstring str{s};
_assign (str);
2015-05-23 13:35:12 +02:00
return *this;
}
//----------------------------------------------------------------------
FString& FString::setNumber (lDouble f_num, int precision)
{
2020-10-14 17:31:52 +02:00
std::array<wchar_t, 20> format{}; // = "%.<precision>Lg"
2019-08-25 22:16:00 +02:00
wchar_t* s = &format[0];
*s++ = L'%';
*s++ = L'.';
2017-09-20 02:51:17 +02:00
// The precision can not have more than 2 digits
if ( precision > 99 )
precision = 99;
if ( precision >= 10 )
{
2017-09-20 02:51:17 +02:00
// The precision value is 2 digits long
*s++ = precision / 10 + L'0';
*s++ = precision % 10 + L'0';
}
else
2017-09-20 02:51:17 +02:00
{
// The precision value has only 1 digit
*s++ = precision + L'0';
2017-09-20 02:51:17 +02:00
}
*s++ = L'L';
*s++ = L'g';
2017-09-20 02:51:17 +02:00
*s = L'\0';
2020-10-14 17:31:52 +02:00
return sprintf(format.data(), f_num);
}
//----------------------------------------------------------------------
FString& FString::setFormatedNumber (sInt64 num, char separator)
2015-05-23 13:35:12 +02:00
{
2019-08-25 22:16:00 +02:00
int n{0};
2020-10-14 17:31:52 +02:00
std::array<wchar_t, 30> buf{};
2020-04-13 12:40:11 +02:00
wchar_t* s = &buf[29]; // Pointer to the last character
auto abs_num = static_cast<uInt64>(num);
2015-05-23 13:35:12 +02:00
if ( separator == 0 )
separator = ' ';
2015-05-23 13:35:12 +02:00
if ( num < 0 )
2019-11-18 16:43:07 +01:00
abs_num = static_cast<uInt64>(-num);
2015-05-23 13:35:12 +02:00
*s = L'\0';
2015-05-23 13:35:12 +02:00
do
{
*--s = L"0123456789"[abs_num % 10];
abs_num /= 10;
2020-04-13 12:40:11 +02:00
n++;
2020-04-13 12:40:11 +02:00
if ( abs_num && n % 3 == 0 )
2015-05-23 13:35:12 +02:00
*--s = separator;
}
while ( abs_num );
2015-05-23 13:35:12 +02:00
if ( num < 0 )
2015-05-23 13:35:12 +02:00
*--s = '-';
2021-06-03 16:08:36 +02:00
std::wstring str{s};
_assign (str);
2015-05-23 13:35:12 +02:00
return *this;
}
//----------------------------------------------------------------------
FString& FString::setFormatedNumber (uInt64 num, char separator)
2015-05-23 13:35:12 +02:00
{
2019-08-25 22:16:00 +02:00
int n{0};
wchar_t buf[30]{};
2020-04-13 12:40:11 +02:00
wchar_t* s = &buf[29]; // Pointer to the last character
2015-05-23 13:35:12 +02:00
*s = L'\0';
if ( separator == 0 )
separator = ' ';
2015-05-23 13:35:12 +02:00
do
{
*--s = L"0123456789"[num % 10];
2015-05-23 13:35:12 +02:00
num /= 10;
2020-04-13 12:40:11 +02:00
n++;
2020-04-13 12:40:11 +02:00
if ( num && n % 3 == 0 )
2015-05-23 13:35:12 +02:00
*--s = separator;
}
while ( num );
2015-05-23 13:35:12 +02:00
2021-06-03 16:08:36 +02:00
std::wstring str{s};
_assign (str);
2015-05-23 13:35:12 +02:00
return *this;
}
// FString operators
//----------------------------------------------------------------------
bool FString::operator < (const FString& s) const
{
return string < s.string;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
bool FString::operator <= (const FString& s) const
{
return string <= s.string;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
bool FString::operator == (const FString& s) const
{
return string == s.string;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
bool FString::operator != (const FString& s) const
{
return ! ( *this == s );
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
bool FString::operator >= (const FString& s) const
{
return string >= s.string;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
bool FString::operator > (const FString& s) const
{
return string > s.string;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
const FString& FString::insert (const FString& s, int pos)
{
if ( isNegative(pos) || uInt(pos) > string.length() )
2015-05-23 13:35:12 +02:00
throw std::out_of_range("");
string.insert(uInt(pos), s.string, 0 , s.getLength());
return *this;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
const FString& FString::insert (const FString& s, std::size_t pos)
2015-05-23 13:35:12 +02:00
{
if ( pos > string.length() )
2015-05-23 13:35:12 +02:00
throw std::out_of_range("");
string.insert(uInt(pos), s.string, 0 , s.getLength());
return *this;
2015-05-23 13:35:12 +02:00
}
//----------------------------------------------------------------------
FString FString::replace (const FString& from, const FString& to) const
2015-05-23 13:35:12 +02:00
{
// Handle empty strings
if ( isEmpty() || from.isEmpty() )
return *this;
2018-03-08 17:57:17 +01:00
FString s{*this};
std::wstring::size_type pos{};
while ( (pos = s.string.find(from.string, pos)) != std::wstring::npos )
2015-05-23 13:35:12 +02:00
{
s.string.replace (pos, from.getLength(), to.string);
pos += to.getLength();
2015-05-23 13:35:12 +02:00
}
2015-05-23 13:35:12 +02:00
return s;
}
//----------------------------------------------------------------------
FString FString::replaceControlCodes() const
2015-05-23 13:35:12 +02:00
{
FString s{*this};
for (auto&& c : s)
{
if ( c <= L'\x1f' )
{
c += L'\x2400';
}
else if ( c == L'\x7f' )
{
c = L'\x2421';
}
else if ( c >= L'\x80' && c <= L'\x9f' )
{
c = L' ';
}
else if ( ! std::iswprint(std::wint_t(c)) )
c = L' ';
}
return s;
}
//----------------------------------------------------------------------
FString FString::expandTabs (int tabstop) const
{
2017-03-26 20:40:04 +02:00
if ( tabstop <= 0 )
2021-06-03 16:08:36 +02:00
return *this;
2017-03-26 20:40:04 +02:00
2021-06-03 16:08:36 +02:00
FString outstr{};
const auto tab_split = split(L"\t");
const auto last = tab_split.end() - 1;
auto iter = tab_split.begin();
2021-06-03 16:08:36 +02:00
while ( iter != tab_split.end() )
{
2021-06-03 16:08:36 +02:00
if ( iter != last )
{
const auto len = iter->getLength();
const auto tab_len = std::size_t(tabstop);
outstr += *iter + std::wstring(tab_len - (len % tab_len), L' ');
}
2018-03-08 17:57:17 +01:00
else
2021-06-03 16:08:36 +02:00
outstr += *iter;
++iter;
}
return outstr;
}
//----------------------------------------------------------------------
FString FString::removeDel() const
{
FString s{*this};
std::size_t i{0};
std::size_t count{0};
for (auto&& c : s)
{
if ( c == 0x7f )
{
count++;
}
else if ( count > 0 )
{
count--;
}
else // count == 0
{
s.string[i] = c;
i++;
}
}
s.string.erase(i);
return s;
}
//----------------------------------------------------------------------
FString FString::removeBackspaces() const
{
FString s{*this};
std::size_t i{0};
for (auto&& c : s)
{
if ( c != L'\b' )
{
s.string[i] = c;
i++;
}
else if ( i > 0 )
{
i--;
}
}
s.string.erase(i);
return s;
}
2018-03-10 05:27:55 +01:00
//----------------------------------------------------------------------
const FString& FString::overwrite (const FString& s, int pos)
{
if ( pos < 0 )
return overwrite (s, 0);
return overwrite (s, std::size_t(pos));
2018-03-10 05:27:55 +01:00
}
//----------------------------------------------------------------------
const FString& FString::overwrite (const FString& s, std::size_t pos)
{
if ( pos > string.length() )
pos = string.length();
string.replace(pos, s.getLength(), s.string);
return *this;
}
2018-03-10 05:27:55 +01:00
//----------------------------------------------------------------------
const FString& FString::remove (std::size_t pos, std::size_t len)
{
auto length = string.length();
2018-03-10 05:27:55 +01:00
if ( pos > length )
return *this;
if ( pos + len > length )
len = length - pos;
string.erase (pos, len);
return *this;
}
//----------------------------------------------------------------------
2018-03-10 05:27:55 +01:00
bool FString::includes (const FString& s) const
{
if ( s.isEmpty() )
2018-03-10 05:27:55 +01:00
return false;
return string.find(s.string) != std::wstring::npos;
}
// private methods of FString
//----------------------------------------------------------------------
2021-06-03 16:08:36 +02:00
inline void FString::_assign (std::wstring& s)
{
if ( string == s )
return;
2021-06-03 16:08:36 +02:00
s.swap(string);
}
//----------------------------------------------------------------------
inline std::string FString::_toCharString (const std::wstring& s) const
{
if ( s.empty() )
return {};
const wchar_t* src = s.c_str();
2020-08-12 22:28:02 +02:00
auto state = std::mbstate_t();
auto size = std::wcsrtombs(nullptr, &src, 0, &state) + 1;
std::vector<char> dest(size);
const auto mblength = std::wcsrtombs (dest.data(), &src, size, &state);
2020-08-12 22:28:02 +02:00
if ( mblength == static_cast<std::size_t>(-1) && errno != EILSEQ )
{
return {};
}
return dest.data();
}
//----------------------------------------------------------------------
inline std::wstring FString::_toWideString (const std::string& s) const
{
if ( s.empty() )
return {};
const char* src = s.c_str();
2020-08-12 22:28:02 +02:00
auto state = std::mbstate_t();
auto size = std::mbsrtowcs(nullptr, &src, 0, &state) + 1;
if ( size == 0 ) // ...malformed UTF-8 string
return {};
std::vector<wchar_t> dest(size);
const auto wide_length = std::mbsrtowcs (dest.data(), &src, size, &state);
2015-05-23 13:35:12 +02:00
if ( wide_length == static_cast<std::size_t>(-1) )
2015-05-23 13:35:12 +02:00
{
if ( src != s.c_str() )
return dest.data();
else
2015-05-23 13:35:12 +02:00
{
return {};
2015-05-23 13:35:12 +02:00
}
}
if ( wide_length == size )
dest[size - 1] = '\0';
if ( wide_length != 0 )
return dest.data();
else
return {};
2015-05-23 13:35:12 +02:00
}
// FString non-member operators
2015-05-23 13:35:12 +02:00
//----------------------------------------------------------------------
FString operator + (const FString& s1, const FString& s2)
2015-05-23 13:35:12 +02:00
{
auto tmp = s1.string + s2.string;
return tmp;
2015-05-23 13:35:12 +02:00
}
2017-09-20 02:51:17 +02:00
//----------------------------------------------------------------------
std::ostream& operator << (std::ostream& outstr, const FString& s)
{
const auto width = std::size_t(outstr.width());
2019-09-29 22:28:58 +02:00
if ( s.string.length() > 0 )
2019-09-29 22:28:58 +02:00
{
outstr << s._toCharString(s.string).c_str();
2019-09-29 22:28:58 +02:00
}
else if ( width > 0 )
{
2020-05-03 13:45:47 +02:00
const FString fill_str{width, wchar_t(outstr.fill())};
outstr << s._toCharString(fill_str.string).c_str();
2019-09-29 22:28:58 +02:00
}
2017-09-20 02:51:17 +02:00
return outstr;
}
//----------------------------------------------------------------------
std::istream& operator >> (std::istream& instr, FString& s)
{
2020-10-14 17:31:52 +02:00
std::array<char, FString::INPBUFFER + 1> buf{};
instr.getline (buf.data(), FString::INPBUFFER);
2021-06-03 16:08:36 +02:00
auto wide_string = s._toWideString(buf.data());
2017-09-20 02:51:17 +02:00
if ( ! wide_string.empty() )
2017-09-20 02:51:17 +02:00
{
s._assign (wide_string);
2017-09-20 02:51:17 +02:00
}
return instr;
}
//----------------------------------------------------------------------
std::wostream& operator << (std::wostream& outstr, const FString& s)
{
const auto width = std::size_t(outstr.width());
2019-09-29 22:28:58 +02:00
if ( s.string.length() > 0 )
2019-09-29 22:28:58 +02:00
{
2017-09-20 02:51:17 +02:00
outstr << s.string;
2019-09-29 22:28:58 +02:00
}
else if ( width > 0 )
{
const FString fill_str{width, outstr.fill()};
2019-09-29 22:28:58 +02:00
outstr << fill_str.string;
}
2017-09-20 02:51:17 +02:00
return outstr;
}
//----------------------------------------------------------------------
std::wistream& operator >> (std::wistream& instr, FString& s)
{
2020-10-14 17:31:52 +02:00
std::array<wchar_t, FString::INPBUFFER + 1> buf{};
instr.getline (buf.data(), FString::INPBUFFER);
2021-06-03 16:08:36 +02:00
std::wstring str(buf.data());
s._assign (str);
2017-09-20 02:51:17 +02:00
return instr;
}
} // namespace finalcut