1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
|
///\file
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2020 John Wellbelove, John Lagerquist
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef ETL_STRING_UTILITIES_INCLUDED
#define ETL_STRING_UTILITIES_INCLUDED
#include "platform.h"
#include "algorithm.h"
#include "enum_type.h"
#include "memory.h"
#include "char_traits.h"
#include "optional.h"
#include <ctype.h>
#include <stdint.h>
#include "private/minmax_push.h"
namespace etl
{
//***************************************************************************
/// string_pad_direction
//***************************************************************************
struct string_pad_direction
{
enum enum_type
{
LEFT,
RIGHT,
};
ETL_DECLARE_ENUM_TYPE(string_pad_direction, int)
ETL_ENUM_TYPE(LEFT, "left")
ETL_ENUM_TYPE(RIGHT, "right")
ETL_END_ENUM_TYPE
};
//***************************************************************************
/// whitespace
//***************************************************************************
template <typename TChar>
struct whitespace;
template <>
struct whitespace<char>
{
static ETL_CONSTEXPR const char* value()
{
return " \t\n\r\f\v";
}
};
#if ETL_USING_CPP20
template <>
struct whitespace<char8_t>
{
static ETL_CONSTEXPR const char8_t* value()
{
return u8" \t\n\r\f\v";
}
};
#endif
template <>
struct whitespace<wchar_t>
{
static ETL_CONSTEXPR const wchar_t* value()
{
return L" \t\n\r\f\v";
}
};
#if ETL_USING_CPP11
template <>
struct whitespace<char16_t>
{
static ETL_CONSTEXPR const char16_t* value()
{
return u" \t\n\r\f\v";
}
};
template <>
struct whitespace<char32_t>
{
static ETL_CONSTEXPR const char32_t* value()
{
return U" \t\n\r\f\v";
}
};
#endif
#if ETL_USING_CPP17
template <typename TChar>
inline constexpr const TChar* whitespace_v = whitespace<TChar>::value();
#endif
//***************************************************************************
/// trim_from_left
/// Trim left of trim_characters
//***************************************************************************
template <typename TIString>
void trim_from_left(TIString& s, typename TIString::const_pointer trim_characters)
{
typename TIString::size_type position = s.find_first_not_of(trim_characters);
s.erase(0U, position);
}
//***************************************************************************
/// trim_whitespace_left
/// Trim left of whitespace
//***************************************************************************
template <typename TIString>
void trim_whitespace_left(TIString& s)
{
trim_from_left(s, whitespace<typename TIString::value_type>::value());
}
//***************************************************************************
/// trim_from_view_left
/// Trim left of trim_characters
//***************************************************************************
template <typename TStringView>
TStringView trim_from_view_left(const TStringView& view, typename TStringView::const_pointer trim_characters)
{
typename TStringView::size_type first = view.find_first_not_of(trim_characters);
typename TStringView::const_pointer pbegin = view.data() + view.size();
if (first != TStringView::npos)
{
pbegin = view.data() + first;
}
return TStringView(pbegin, etl::distance(pbegin, view.data() + view.size()));
}
//***************************************************************************
/// trim_view_whitespace_left
/// Trim left of whitespace
//***************************************************************************
template <typename TStringView>
TStringView trim_view_whitespace_left(TStringView& s)
{
return trim_from_view_left(s, whitespace<typename TStringView::value_type>::value());
}
//***************************************************************************
/// trim_left
/// Trim left, up to, but not including, delimiters.
//***************************************************************************
template <typename TIString>
void trim_left(TIString& s, typename TIString::const_pointer delimiters)
{
typename TIString::size_type p = s.find_first_of(delimiters);
if (p == TIString::npos)
{
s.clear();
}
else
{
s.erase(0, p);
}
}
//***************************************************************************
/// trim_view_left
/// View trim left, up to, but not including, delimiters.
//***************************************************************************
template <typename TStringView>
TStringView trim_view_left(const TStringView& view, typename TStringView::const_pointer delimiters)
{
typename TStringView::size_type first = view.find_first_of(delimiters);
typename TStringView::const_pointer pbegin = view.data();
if (first != TStringView::npos)
{
pbegin += first;
return TStringView(pbegin, view.size() - first);
}
else
{
return TStringView(pbegin, typename TStringView::size_type(0U));
}
}
//***************************************************************************
/// trim_from_right
/// Trim right of trim_characters
//***************************************************************************
template <typename TIString>
void trim_from_right(TIString& s, typename TIString::const_pointer trim_characters)
{
s.erase(s.find_last_not_of(trim_characters) + 1);
}
//***************************************************************************
/// trim_whitespace_right
/// Trim from right of whitespace
//***************************************************************************
template <typename TIString>
void trim_whitespace_right(TIString& s)
{
trim_from_right(s, whitespace<typename TIString::value_type>::value());
}
//***************************************************************************
/// trim_from_view_right
/// Trim right of trim_characters
//***************************************************************************
template <typename TStringView>
TStringView trim_from_view_right(const TStringView& view, typename TStringView::const_pointer trim_characters)
{
typename TStringView::size_type last = view.find_last_not_of(trim_characters) + 1;
typename TStringView::const_pointer pend = view.data();
if (last != TStringView::npos)
{
pend += last;
}
return TStringView(view.data(), etl::distance(view.data(), pend));
}
//***************************************************************************
/// trim_view_whitespace_right
/// Trim right of whitespace
//***************************************************************************
template <typename TStringView>
TStringView trim_view_whitespace_right(TStringView& view)
{
return trim_from_view_right(view, whitespace<typename TStringView::value_type>::value());
}
//***************************************************************************
/// trim_right
//***************************************************************************
template <typename TIString>
void trim_right(TIString& s, typename TIString::const_pointer delimiters)
{
typename TIString::size_type p = s.find_last_of(delimiters);
if (p == TIString::npos)
{
s.clear();
}
else
{
++p;
if (p != s.size())
{
s.erase(p);
}
}
}
//***************************************************************************
/// trim_view_right
//***************************************************************************
template <typename TStringView>
TStringView trim_view_right(const TStringView& view, typename TStringView::const_pointer delimiters)
{
typename TStringView::size_type last = view.find_last_of(delimiters) + 1;
typename TStringView::const_pointer pend = view.data();
if (last != TStringView::npos)
{
pend += last;
return TStringView(view.data(), etl::distance(view.data(), pend));
}
else
{
return TStringView(view.data(), typename TStringView::size_type(0U));
}
}
//***************************************************************************
/// trim_from
/// Trim left and right of trim_characters
//***************************************************************************
template <typename TIString>
void trim_from(TIString& s, typename TIString::const_pointer trim_characters)
{
trim_from_left(s, trim_characters);
trim_from_right(s, trim_characters);
}
//***************************************************************************
/// trim_whitespace
/// Trim both ends of whitespace
//***************************************************************************
template <typename TIString>
void trim_whitespace(TIString& s)
{
trim_from(s, whitespace<typename TIString::value_type>::value());
}
//***************************************************************************
/// trim_from_view
/// Trim left and right of trim_characters
//***************************************************************************
template <typename TStringView>
TStringView trim_from_view(const TStringView& view, typename TStringView::const_pointer trim_characters)
{
typename TStringView::size_type first = view.find_first_not_of(trim_characters);
typename TStringView::size_type last = view.find_last_not_of(trim_characters) + 1;
typename TStringView::const_pointer pbegin = view.data();
typename TStringView::const_pointer pend = view.data();
if (first != TStringView::npos)
{
pbegin += first;
}
if (last != TStringView::npos)
{
pend += last;
}
return TStringView(pbegin, etl::distance(pbegin, pend));
}
//***************************************************************************
/// trim_view_whitespace
/// Trim both ends of whitespace
//***************************************************************************
template <typename TStringView>
TStringView trim_view_whitespace(const TStringView& view)
{
return trim_from_view(view, whitespace<typename TStringView::value_type>::value());
}
//***************************************************************************
/// trim_delimiters
/// Trim left and right of trim_characters
//***************************************************************************
template <typename TIString>
void trim(TIString& s, typename TIString::const_pointer delimiters)
{
trim_left(s, delimiters);
trim_right(s, delimiters);
}
//***************************************************************************
/// trim_view
/// Trim left and right of trim_characters
//***************************************************************************
template <typename TStringView>
TStringView trim_view(const TStringView& view, typename TStringView::const_pointer delimiters)
{
typename TStringView::size_type first = view.find_first_of(delimiters);
typename TStringView::size_type last = view.find_last_of(delimiters) + 1;
typename TStringView::const_pointer pbegin = view.data();
typename TStringView::const_pointer pend = view.data();
if (first != TStringView::npos)
{
pbegin += first;
}
if (last != TStringView::npos)
{
pend += last;
}
return TStringView(pbegin, etl::distance(pbegin, pend));
}
//***************************************************************************
/// Get up to the first n characters.
//***************************************************************************
template <typename TIString>
void left_n(TIString& s, typename TIString::size_type n)
{
n = (n > s.size()) ? s.size() : n;
s.erase(s.begin() + n, s.end());
}
//***************************************************************************
/// Get a view of up to the first n characters.
//***************************************************************************
template <typename TStringView>
TStringView left_n_view(const TStringView& view, typename TStringView::size_type n)
{
n = (n > view.size()) ? view.size() : n;
return TStringView(etl::addressof(*view.begin()), n);
}
//***************************************************************************
/// Get up to the last n characters.
//***************************************************************************
template <typename TIString>
void right_n(TIString& s, typename TIString::size_type n)
{
n = (n > s.size()) ? s.size() : n;
s.erase(s.begin(), s.end() - n);
}
//***************************************************************************
/// Get a view of up to the last n characters.
//***************************************************************************
template <typename TStringView>
TStringView right_n_view(const TStringView& view, typename TStringView::size_type n)
{
n = (n > view.size()) ? view.size() : n;
return TStringView(view.data() + view.size() - n, n);
}
//***************************************************************************
/// reverse
/// Reverse a string
//***************************************************************************
template <typename TIString>
void reverse(TIString& s)
{
etl::reverse(s.begin(), s.end());
}
//***************************************************************************
/// replace_characters
//***************************************************************************
template <typename TIString, typename TPair>
void replace_characters(TIString& s,
const TPair* pairsbegin,
const TPair* pairsend)
{
while (pairsbegin != pairsend)
{
etl::replace(s.begin(), s.end(), pairsbegin->first, pairsbegin->second);
++pairsbegin;
}
}
//***************************************************************************
/// replace_strings
//***************************************************************************
template <typename TIString, typename TPair>
void replace_strings(TIString& s,
const TPair* pairsbegin,
const TPair* pairsend)
{
while (pairsbegin != pairsend)
{
const typename TIString::value_type* p_old = pairsbegin->first;
const typename TIString::value_type* p_new = pairsbegin->second;
typename TIString::size_type position = 0U;
do
{
position = s.find(p_old, position);
if (position != TIString::npos)
{
s.replace(position, typename TIString::size_type(etl::strlen(p_old)), p_new, typename TIString::size_type(etl::strlen(p_new)));
position += typename TIString::size_type(etl::strlen(p_new));
}
} while (position != TIString::npos);
++pairsbegin;
}
}
//*********************************************************************
/// Find first of any of delimiters within the string
//*********************************************************************
template <typename TIterator, typename TPointer>
TIterator find_first_of(TIterator first, TIterator last, TPointer delimiters)
{
TIterator itr(first);
while (itr != last)
{
TPointer pd = delimiters;
while (*pd != 0)
{
if (*itr == *pd)
{
return itr;
}
++pd;
}
++itr;
}
return last;
}
//*********************************************************************
/// Find first of any of delimiters within the string
//*********************************************************************
template <typename TIString, typename TPointer>
typename TIString::iterator find_first_of(TIString& s, TPointer delimiters)
{
return find_first_of(s.begin(), s.end(), delimiters);
}
//*********************************************************************
/// Find first of any of delimiters within the string
//*********************************************************************
template <typename TIString, typename TPointer>
typename TIString::const_iterator find_first_of(const TIString& s, TPointer delimiters)
{
return find_first_of(s.begin(), s.end(), delimiters);
}
//*********************************************************************
/// Find first not of any of delimiters within the string
//*********************************************************************
template <typename TIterator, typename TPointer>
TIterator find_first_not_of(TIterator first, TIterator last, TPointer delimiters)
{
TIterator itr(first);
while (itr != last)
{
TPointer pd = delimiters;
bool found = false;
while (*pd != 0)
{
if (*itr == *pd)
{
found = true;
break;
}
++pd;
}
if (!found)
{
return itr;
}
++itr;
}
return last;
}
//*********************************************************************
/// Find first not of any of delimiters within the string
//*********************************************************************
template <typename TIString, typename TPointer>
typename TIString::iterator find_first_not_of(TIString& s, TPointer delimiters)
{
return find_first_not_of(s.begin(), s.end(), delimiters);
}
//*********************************************************************
/// Find first not of any of delimiters within the string
//*********************************************************************
template <typename TIString, typename TPointer>
typename TIString::const_iterator find_first_not_of(const TIString& s, TPointer delimiters)
{
return find_first_not_of(s.begin(), s.end(), delimiters);
}
//*********************************************************************
/// Find last of any of delimiters within the string
//*********************************************************************
template <typename TIterator, typename TPointer>
TIterator find_last_of(TIterator first, TIterator last, TPointer delimiters)
{
if (first == last)
{
return last;
}
TIterator itr(last);
TIterator end(first);
do
{
--itr;
TPointer pd = delimiters;
while (*pd != 0)
{
if (*itr == *pd)
{
return itr;
}
++pd;
}
} while (itr != end);
return last;
}
//*********************************************************************
/// Find last of any of delimiters within the string
//*********************************************************************
template <typename TIString, typename TPointer>
typename TIString::iterator find_last_of(TIString& s, TPointer delimiters)
{
return find_last_of(s.begin(), s.end(), delimiters);
}
//*********************************************************************
/// Find last of any of delimiters within the string
//*********************************************************************
template <typename TIString, typename TPointer>
typename TIString::const_iterator find_last_of(const TIString& s, TPointer delimiters)
{
return find_last_of(s.begin(), s.end(), delimiters);
}
//*********************************************************************
/// Find last not of any of delimiters within the string
//*********************************************************************
template <typename TIterator, typename TPointer>
TIterator find_last_not_of(TIterator first, TIterator last, TPointer delimiters)
{
if (first == last)
{
return last;
}
TIterator itr(last);
TIterator end(first);
do
{
--itr;
TPointer pd = delimiters;
bool found = false;
while (*pd != 0)
{
if (*itr == *pd)
{
found = true;
break;
}
++pd;
}
if (!found)
{
return itr;
}
} while (itr != end);
return last;
}
//*********************************************************************
/// Find last not of any of delimiters within the string
//*********************************************************************
template <typename TIString, typename TPointer>
typename TIString::iterator find_last_not_of(TIString& s, TPointer delimiters)
{
return find_last_not_of(s.begin(), s.end(), delimiters);
}
//*********************************************************************
/// Find last not of any of delimiters within the string
//*********************************************************************
template <typename TIString, typename TPointer>
typename TIString::const_iterator find_last_not_of(const TIString& s, TPointer delimiters)
{
return find_last_not_of(s.begin(), s.end(), delimiters);
}
//***************************************************************************
/// get_token
//***************************************************************************
template <typename TInput, typename TStringView>
etl::optional<TStringView> get_token(const TInput& input, typename TInput::const_pointer delimiters, const etl::optional<TStringView>& last_view, bool ignore_empty_tokens)
{
typedef typename TInput::const_pointer const_pointer;
bool token_found = false;
typename TStringView::size_type position = 0U;
TStringView view = last_view.value_or(TStringView());
const_pointer begin_ptr = input.data();
if (begin_ptr == ETL_NULLPTR)
{
return etl::optional<TStringView>();
}
const_pointer end_ptr = begin_ptr + input.size();
while (!token_found)
{
// Does the last view have valid data?
if (view.data() != ETL_NULLPTR)
{
position = etl::distance(begin_ptr, view.data() + view.size() + 1U);
// Have we reached the end of the string?
if (position > input.size())
{
return etl::optional<TStringView>();
}
}
// Look for the next token.
const_pointer first_ptr = begin_ptr + position;
const_pointer last_ptr = find_first_of(first_ptr, end_ptr, delimiters);
view = TStringView(first_ptr, etl::distance(first_ptr, last_ptr));
token_found = ((view.size() != 0U) || !ignore_empty_tokens);
}
return etl::optional<TStringView>(view);
}
//***************************************************************************
/// pad_left
//***************************************************************************
template <typename TIString>
void pad_left(TIString& s, typename TIString::size_type required_size, typename TIString::value_type pad_char)
{
required_size = etl::min(required_size, s.max_size());
if (required_size > s.size())
{
required_size -= s.size();
s.insert(typename TIString::size_type(0U), required_size, pad_char);
}
}
//***************************************************************************
/// pad_right
//***************************************************************************
template <typename TIString>
void pad_right(TIString& s, typename TIString::size_type required_size, typename TIString::value_type pad_char)
{
required_size = etl::min(required_size, s.max_size());
if (required_size > s.size())
{
required_size -= s.size();
s.insert(s.size(), required_size, pad_char);
}
}
//***************************************************************************
/// pad
//***************************************************************************
template <typename TIString>
void pad(TIString& s, typename TIString::size_type required_size, string_pad_direction pad_direction, typename TIString::value_type pad_char)
{
switch (int(pad_direction))
{
case string_pad_direction::LEFT:
{
pad_left(s, required_size, pad_char);
break;
}
case string_pad_direction::RIGHT:
{
pad_right(s, required_size, pad_char);
break;
}
default:
{
break;
}
}
}
//***************************************************************************
/// to_upper_case
//***************************************************************************
template <typename TString>
void to_upper_case(TString& s)
{
etl::transform(s.begin(), s.end(), s.begin(), ::toupper);
}
//***************************************************************************
/// to_lower_case
//***************************************************************************
template <typename TString>
void to_lower_case(TString& s)
{
etl::transform(s.begin(), s.end(), s.begin(), ::tolower);
}
//***************************************************************************
/// to_sentence_case
//***************************************************************************
template <typename TString>
void to_sentence_case(TString& s)
{
typename TString::iterator itr = s.begin();
*itr = typename TString::value_type(::toupper(*itr));
++itr;
etl::transform(itr, s.end(), itr, ::tolower);
}
}
#include "private/minmax_pop.h"
#endif
|