2 Copyright (C) 2014 Waves Audio Ltd.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #ifndef __WCFixedString_h__
21 #define __WCFixedString_h__
24 #include "WCFixedString.h"
26 // do not #include anything else here but standard C++ library files, this file should be free from any and all depandencies
27 // do not put any DEBUG_s or TRACE_s in this file, since it is used in BgkConsole functions
38 #include "BasicTypes/WUDefines.h"
39 #include "BasicTypes/WTByteOrder.h"
40 #include "WavesPublicAPI/wstdint.h"
41 #include "MiscUtils/MinMaxUtilities.h"
43 // use this macro instead of std :: string to mark the that use of std :: string could not be replaced
45 #define std_string_approved std::string
48 const char* const kStrNewLine = "\n";
50 #ifdef PLATFORM_WINDOWS
51 const char* const kStrNewLine = "\r\n";
54 class DllExport WCFixedStringBase
58 typedef intptr_t spos_t; // signed position, defined to intptr_t because Windows does not have ssize_t
59 static const pos_t npos = UINTPTR_MAX; // Same as size_max
61 WCFixedStringBase(char* const in_begin, const size_t in_MaxFixedStringLength) :
63 m_MaxFixedStringLength(in_MaxFixedStringLength),
69 inline WCFixedStringBase& operator=(const WCFixedStringBase& in_fixedStrToAssign)
71 if (this != &in_fixedStrToAssign)
74 operator<<(in_fixedStrToAssign);
80 inline WCFixedStringBase& operator=(const char* in_CStrToAssign)
83 operator<<(in_CStrToAssign);
88 inline WCFixedStringBase& operator=(const char in_charToAssign)
91 operator<<(in_charToAssign);
96 char operator[](const pos_t in_index) const
98 if (in_index < m_MaxFixedStringLength)
99 return m_begin[in_index];
101 return m_begin[m_MaxFixedStringLength]; // in_index was too big
104 char& operator[](const pos_t in_index)
106 if (in_index < m_MaxFixedStringLength)
107 return m_begin[in_index];
109 return m_begin[m_MaxFixedStringLength]; // in_index was too big
112 inline size_t resize(const size_t in_newSize)
114 m_end = m_begin + WUMin<size_t>(in_newSize, m_MaxFixedStringLength);
121 return m_MaxFixedStringLength;
126 return m_MaxFixedStringLength;
135 inline const char * c_str() const
147 inline size_t size() const
149 return m_end - m_begin;
152 inline char* begin() const
157 inline char* end() const
162 inline size_t length() const
167 inline bool empty() const
169 return m_begin == m_end;
172 inline void reverse(char* in_left, char* in_right)
174 char* left = in_left;
175 char* right = in_right;
178 char temp = *--right;
184 inline void reverse()
186 reverse(m_begin, m_end);
189 inline void to_lower()
191 char* pToDo = m_begin;
193 while (pToDo < m_end)
195 *pToDo = static_cast<char>(std::tolower(*pToDo));
200 inline void to_upper()
202 char* pToDo = m_begin;
204 while (pToDo < m_end)
206 *pToDo = static_cast<char>(std::toupper(*pToDo));
211 // append a single char in_count times
212 inline void append(const char in_charToAppend, const size_t in_count)
215 while ((m_end < m_begin+m_MaxFixedStringLength) && counter++ < in_count)
216 *m_end++ = in_charToAppend;
217 #if kEnableDebug == 1
218 if (counter < in_count) // if there wasn't enough room for some appended chars
220 m_begin[0] = '@'; // mark the string as overflowed
226 inline void append(const char* in_chars)
228 operator<<(in_chars);
231 // append "iterator style"
232 inline void append(const char* in_chars_begin, const char* in_chars_end)
234 const char* curr_char = in_chars_begin;
235 while ((m_end < m_begin+m_MaxFixedStringLength) && curr_char < in_chars_end && *curr_char != '\0')
236 *m_end++ = *curr_char++;
238 #if kEnableDebug == 1
239 if (curr_char < in_chars_end) // if there wasn't enough room for some appended chars
241 m_begin[0] = '@'; // mark the string as overflowed
247 // append from a char* in_count chars, (no \0 is required to terminate the input string)
248 inline void append(const char* in_chars_begin, const size_t in_count)
250 append(in_chars_begin, in_chars_begin + in_count);
253 // assign from a char* in_count chars, (no \0 is required to terminate the input string)
254 inline void assign(const char* in_chars_begin, const size_t in_count)
257 append(in_chars_begin, in_chars_begin + in_count);
260 // assign from a char* , (a \0 is required to terminate the input string)
261 inline void assign(const char* in_chars_ptr)
264 operator<<(in_chars_ptr);
267 // assign from a char* to a char*
268 inline void assign(const char* in_begin, const char* in_end)
270 assign(in_begin, size_t(in_end - in_begin));
273 inline void append_double_with_precision(const double in_double, const int in_precision)
275 const unsigned int tempBufSize = 32;
276 char buf[tempBufSize];
278 #ifdef PLATFORM_WINDOWS
279 _snprintf_s(buf, tempBufSize, tempBufSize - 1, "%.*f", in_precision, in_double);
282 std::snprintf(buf, tempBufSize, "%.*f", in_precision, in_double);
285 snprintf(buf, tempBufSize, "%.*f", in_precision, in_double);
291 inline void append_uint(const uint64_t in_uint, const int_fast16_t in_base = 10)
293 uint_fast64_t num = in_uint;
295 char* lasr_char_before = m_end;
298 char remainder(static_cast<char>(num % in_base));
300 if ( remainder < 10 )
301 operator<<(char(remainder + '0'));
303 operator<<(char(remainder - 10 + 'A'));
308 reverse(lasr_char_before, m_end);
311 inline void append_hex_binary(const uint8_t* in_binary, const size_t in_size)
313 static const char hexdigits[] = "0123456789ABCDEF";
315 #if _BYTEORDER_BIG_ENDIAN==1
316 for (size_t ibyte = 0; ibyte < in_size; ++ibyte)
317 #elif _BYTEORDER_BIG_ENDIAN==0
318 for (size_t ibyte = in_size; ibyte > 0; --ibyte)
321 operator<<(hexdigits[in_binary[ibyte - 1] >> 4]);
322 operator<<(hexdigits[in_binary[ibyte - 1] & 0x0F]);
326 inline WCFixedStringBase& operator<<(const char in_charToAppend)
328 if (m_end < m_begin+m_MaxFixedStringLength)
329 *m_end++ = in_charToAppend;
330 #if kEnableDebug == 1
331 else // if there wasn't enough room for the appended char
333 m_begin[0] = '@'; // mark the string as overflowed
342 inline WCFixedStringBase& operator<<(const char* const in_strToAppend)
344 if (0 != in_strToAppend)
346 const char* pSource = in_strToAppend;
348 while (*pSource != '\0' && m_end < m_begin+m_MaxFixedStringLength)
349 *m_end++ = *pSource++;
351 #if kEnableDebug == 1
352 if (*pSource != '\0') // if there wasn't enough room for some appended chars
354 m_begin[0] = '@'; // mark the string as overflowed
363 WCFixedStringBase& operator<<(const uint64_t in_uint)
365 append_uint(in_uint, 10);
371 // Warning prevention: the operator<< function overload for unsigneds used to create lots
372 // of warnings once size_t usage was becoming widespread. So for each OS we define only
373 // those overloads that are actually needed. On Windows 32 bit we still get
374 // 'warning C4267: 'argument' : conversion from 'size_t' to 'const unsigned int', possible loss of data'
375 // warning which we do not know how to solve yet. The function DummyFunctionsForWarningTest
376 // in file WCFixedStringStream.cpp calls all combinations of operator<<(unsigned something)
377 // And should produce no warnings - (except the C4267 on windows).
378 #if defined(__APPLE__) // both 32 & 64 bit
379 WCFixedStringBase& operator<<(const size_t in_uint) {
380 return operator<<(static_cast<unsigned long long>(in_uint));
383 // WCFixedStringBase& operator<<(const unsigned char in_uint) {
384 // return operator<<(static_cast<const unsigned long long>(in_uint));
387 // WCFixedStringBase& operator<<(const size_t in_uint) {
388 // return operator<<(static_cast<const uint64_t>(in_uint));
391 #if defined(__APPLE__) || defined(PLATFORM_WINDOWS) || defined(__linux__) // both 32 & 64 bit
392 WCFixedStringBase& operator<<(const unsigned int in_uint) {
393 return operator<<(static_cast<uint64_t>(in_uint));
397 #if defined(PLATFORM_WINDOWS) || defined(__linux__) // both 32 & 64 bit
398 WCFixedStringBase& operator<<(const unsigned long in_uint) {
399 return operator<<(static_cast<uint64_t>(in_uint));
403 WCFixedStringBase& operator<<(const long long in_int)
407 #ifdef PLATFORM_WINDOWS
408 // uintmax_t unsigned_in_num = _abs64(in_int);
409 uintmax_t unsigned_in_num = in_int < 0 ? static_cast<uintmax_t>(-in_int) : static_cast<uintmax_t>(in_int);
411 uintmax_t unsigned_in_num = std::abs(in_int);
413 append_uint(unsigned_in_num, 10);
418 WCFixedStringBase& operator<<(const short in_int) {
419 return operator<<(static_cast<int64_t>(in_int));
422 WCFixedStringBase& operator<<(const int in_int) {
423 return operator<<(static_cast<int64_t>(in_int));
426 WCFixedStringBase& operator<<(const long in_int) {
427 return operator<<(static_cast<int64_t>(in_int));
430 WCFixedStringBase& operator<<(const double in_doubleToWrite)
432 append_double_with_precision(in_doubleToWrite, 10);
437 WCFixedStringBase& operator<<(const float in_floatToWrite)
439 append_double_with_precision(double(in_floatToWrite), 5);
444 inline WCFixedStringBase& operator<<(const WCFixedStringBase& in_fixedStrToAppend)
446 operator<<(in_fixedStrToAppend.c_str());
451 WCFixedStringBase& operator<< (bool abool)
453 return abool ? operator<<("true") : operator<<("false");
456 template<typename T> WCFixedStringBase& operator+=(T in_type)
458 return operator<<(in_type);
461 ptrdiff_t compare(const char* in_to_compare) const
463 ptrdiff_t retVal = 1;
465 if (0 != in_to_compare)
467 retVal = strcmp(c_str(), in_to_compare);
474 ptrdiff_t compare(const WCFixedStringBase& in_to_compare) const
476 ptrdiff_t retVal = compare(in_to_compare.c_str());
480 ptrdiff_t case_insensitive_compare(const char* in_to_compare) const
482 ptrdiff_t retVal = 1;
484 if (0 != in_to_compare)
486 #ifdef PLATFORM_WINDOWS
487 retVal = _stricmp(c_str(), in_to_compare);
489 #if defined(__linux__) || defined(__APPLE__)
490 retVal = strcasecmp(c_str(), in_to_compare);
497 ptrdiff_t case_insensitive_compare(const WCFixedStringBase& in_to_compare) const
499 ptrdiff_t retVal = case_insensitive_compare(in_to_compare.c_str());
503 pos_t find(const char in_char_to_find) const
505 const char* pCurrChar = m_begin;
506 while (pCurrChar < m_end && *pCurrChar != in_char_to_find)
509 return (pCurrChar < m_end) ? (pCurrChar - m_begin) : npos;
512 pos_t rfind(const char in_char_to_find) const
515 const char* pCurrChar = m_end;
517 while (pCurrChar != m_begin)
520 if (*pCurrChar == in_char_to_find)
522 retVal = pCurrChar - m_begin;
530 pos_t find(const char* in_chars_to_find, const pos_t in_start_from = 0) const
533 size_t to_find_size = ::strlen(in_chars_to_find);
535 if (to_find_size > 0 && to_find_size <= size() && in_start_from < size())
537 const char* pCurrChar = m_begin + in_start_from;
538 while ((m_end - pCurrChar) >= (ptrdiff_t)to_find_size)
540 int found = ::memcmp(pCurrChar, in_chars_to_find, to_find_size);
543 retVal = (pCurrChar - m_begin);
554 pos_t rfind(const char* in_chars_to_find) const
557 size_t to_find_size = ::strlen(in_chars_to_find);
559 if (to_find_size > 0 && to_find_size <= size())
561 const char* pCurrChar = m_end - to_find_size;
562 while (m_begin <= pCurrChar)
564 int found = ::memcmp(pCurrChar, in_chars_to_find, to_find_size);
567 retVal = (pCurrChar - m_begin);
578 pos_t find_case_insensitive(const char* in_chars_to_find, const pos_t in_start_from = 0) const
581 size_t to_find_size = ::strlen(in_chars_to_find);
583 if (to_find_size > 0 && to_find_size <= size() && in_start_from < size())
585 const char* pCurrChar = m_begin + in_start_from;
586 while ((m_end - pCurrChar) >= (ptrdiff_t)to_find_size)
589 for (i = 0; i < to_find_size; ++i)
591 if (tolower(*(pCurrChar+i)) != tolower(in_chars_to_find[i]))
595 if (i == to_find_size)
597 retVal = (pCurrChar - m_begin);
608 pos_t find_first_of(const char* in_possibe_chars_to_find, const pos_t in_start_from = 0) const
612 if (in_start_from < size())
614 const char* pFoundChar = strpbrk(m_begin + in_start_from, in_possibe_chars_to_find);
617 retVal = (pFoundChar - m_begin);
624 pos_t find_last_of(const char* in_possibe_chars_to_find, const pos_t in_start_from = 0) const
628 pos_t curr_location = in_start_from;
630 while (size() > curr_location)
632 pos_t found = find_first_of(in_possibe_chars_to_find, curr_location);
636 curr_location = found + 1;
645 pos_t find_first_not_of(const char* in_acceptable_chars, const pos_t in_start_from = 0) const
649 if (in_start_from < size())
651 retVal = (strspn(m_begin + in_start_from, in_acceptable_chars));
652 if (size() <= retVal + in_start_from)
658 retVal += in_start_from;
665 pos_t find_last_not_of(const char* in_acceptable_chars, const pos_t in_start_from = 0) const
669 pos_t curr_location = in_start_from;
671 while (size() > curr_location)
673 pos_t found = find_first_not_of(in_acceptable_chars, curr_location);
677 curr_location = found + 1;
686 // return true if in_begin_text is found at position 0 OR if in_begin_text is empty
687 bool begins_with(const char* in_begin_text) const
689 pos_t where = find(in_begin_text, 0);
690 bool retVal = (0 == where) || (0 == ::strlen(in_begin_text));
694 // return true if in_end_text is found at th end OR if in_end_text is empty
695 bool ends_with(const char* in_end_text) const
697 pos_t where = rfind(in_end_text);
698 bool retVal = ((size() - strlen(in_end_text)) == where) || (0 == ::strlen(in_end_text));
702 size_t replace(const char in_look_for, const char in_replace_with)
706 char* pCurrChar = m_begin;
707 while (pCurrChar < m_end)
709 if (*pCurrChar == in_look_for)
711 *pCurrChar = in_replace_with;
720 // erase in_size chars starting from in_location
721 void erase(const pos_t in_location, const size_t in_num_chars = 1)
723 if (size() > in_location && in_num_chars > 0)
725 size_t actual_num_chars = WUMin(in_num_chars, size_t(size() - in_location));
726 char* pTo = m_begin + in_location;
727 char* pFrom = pTo + actual_num_chars;
729 while (pFrom < m_end)
732 resize(size() - actual_num_chars);
736 // erase any char that appear in in_forbidden_chars
737 void erase_all_of(const char* in_forbidden_chars)
739 pos_t curr_location = 0;
741 while (npos != curr_location)
743 curr_location = find_first_of(in_forbidden_chars, curr_location);
744 if (npos != curr_location)
745 erase(curr_location);
749 // erase any char that do not appear in in_allowed_chars
750 void erase_all_not_of(const char* in_allowed_chars)
752 pos_t curr_location = 0;
754 while (npos != curr_location)
756 curr_location = find_first_not_of(in_allowed_chars, curr_location);
757 if (npos != curr_location)
758 erase(curr_location);
762 //! Copy the content of fixed string to a buffer appending a '\0' at the end.
763 //! If in_buffer_size is more than the allocated buffer size memory over write will happen!
764 void copy_to_buffer(const size_t in_buffer_size, char* out_buffer)
766 if (in_buffer_size > 0 && 0 != out_buffer)
768 char* cur_buffer = out_buffer;
769 const char* cur_fixed = m_begin;
770 const char* end_buffer = out_buffer + (WUMin<size_t>(in_buffer_size - 1, m_end - m_begin));
771 while (cur_buffer < end_buffer)
772 *cur_buffer++ = *cur_fixed++;
779 ~WCFixedStringBase() {}
782 const size_t m_MaxFixedStringLength;
787 WCFixedStringBase(const WCFixedStringBase& in_fixedStrToCopy);
790 m_begin(in_fixedStrToCopy.m_begin),
791 m_MaxFixedStringLength(in_fixedStrToCopy.m_MaxFixedStringLength),
792 m_end(in_fixedStrToCopy.m_end)
798 template<size_t kMaxFixedStringLength> class DllExport WCFixedString : public WCFixedStringBase
802 inline WCFixedString() :
803 WCFixedStringBase(m_fixedString, kMaxFixedStringLength)
807 inline WCFixedString(const char* const in_strToAssign) :
808 WCFixedStringBase(m_fixedString, kMaxFixedStringLength)
810 operator<<(in_strToAssign);
813 inline WCFixedString(const WCFixedStringBase& in_fixedStrToAssign) :
814 WCFixedStringBase(m_fixedString, kMaxFixedStringLength)
816 operator<<(in_fixedStrToAssign);
819 inline WCFixedString(const WCFixedString& in_fixedStrToAssign) :
820 WCFixedStringBase(m_fixedString, kMaxFixedStringLength)
822 operator<<(in_fixedStrToAssign);
825 inline WCFixedString(const char in_char, const size_t in_count = 1) :
826 WCFixedStringBase(m_fixedString, kMaxFixedStringLength)
828 append(in_char, in_count);
831 inline WCFixedString(const char* in_chars, const size_t in_count) :
832 WCFixedStringBase(m_fixedString, kMaxFixedStringLength)
834 append(in_chars, in_count);
837 // substr now supports negative in_length, which means "from the end" so
838 // "abcdefg".substr(1, -1) == "bcdef"
839 inline const WCFixedString substr(const pos_t in_pos = 0, const spos_t in_length = kMaxFixedStringLength) const
841 pos_t adjusted_pos = WUMin<size_t>(in_pos, size());
842 size_t adjusted_length = 0;
845 adjusted_length = size_t(WUMax<spos_t>(0, spos_t(size() - adjusted_pos) + in_length));
848 adjusted_length = WUMin<size_t>(in_length, size() - adjusted_pos);
850 WCFixedString retVal;
851 retVal.append(m_begin + adjusted_pos, adjusted_length);
858 char m_fixedString[kMaxFixedStringLength + 1]; // the "+ 1" is so that *m_end is always valid, and we can put the '\0' there};
861 inline bool operator==(const WCFixedStringBase& in_left, const WCFixedStringBase& in_right)
863 return 0 == in_left.compare(in_right.c_str());
866 inline bool operator==(const WCFixedStringBase& in_left, const char* const in_right)
868 return 0 == in_left.compare(in_right);
871 inline bool operator!=(const WCFixedStringBase& in_left, const WCFixedStringBase& in_right)
873 return 0 != in_left.compare(in_right.c_str());
876 inline bool operator!=(const WCFixedStringBase& in_left, const char* const in_right)
878 return 0 != in_left.compare(in_right);
881 // class WCFixedStringBase
882 typedef WCFixedString<4> WCFixedString4;
883 typedef WCFixedString<15> WCFixedString15;
884 typedef WCFixedString<31> WCFixedString31;
885 typedef WCFixedString<63> WCFixedString63;
886 typedef WCFixedString<127> WCFixedString127;
887 typedef WCFixedString<255> WCFixedString255;
888 typedef WCFixedString<511> WCFixedString511;
889 typedef WCFixedString<1023> WCFixedString1023;
890 typedef WCFixedString<2047> WCFixedString2047;
892 template<size_t kSizeOfFirst, size_t kSizeOfSecond>
893 class WCFixedStringPair : public std::pair< WCFixedString<kSizeOfFirst>, WCFixedString<kSizeOfSecond> >
896 WCFixedStringPair(const char* const in_firstStr = 0, const char* const in_secondStr = 0) :
897 std::pair< WCFixedString<kSizeOfFirst>, WCFixedString<kSizeOfSecond> >(in_firstStr, in_secondStr) {}
898 WCFixedStringPair(const WCFixedStringBase& in_firstStr, const char* const in_secondStr = 0) :
899 std::pair< WCFixedString<kSizeOfFirst>, WCFixedString<kSizeOfSecond> >(in_firstStr, in_secondStr) {}
900 WCFixedStringPair(const WCFixedStringBase& in_firstStr, const WCFixedStringBase& in_secondStr) :
901 std::pair< WCFixedString<kSizeOfFirst>, WCFixedString<kSizeOfSecond> >(in_firstStr, in_secondStr) {}
904 #endif // #ifndef __WCFixedString_h__