diff options
author | Jacek Antonelli | 2008-09-06 18:24:57 -0500 |
---|---|---|
committer | Jacek Antonelli | 2008-09-06 18:25:07 -0500 |
commit | 798d367d54a6c6379ad355bd8345fa40e31e7fe9 (patch) | |
tree | 1921f1708cd0240648c97bc02df2c2ab5f2fc41e /linden/indra/llcommon/llstring.cpp | |
parent | Second Life viewer sources 1.20.15 (diff) | |
download | meta-impy-798d367d54a6c6379ad355bd8345fa40e31e7fe9.zip meta-impy-798d367d54a6c6379ad355bd8345fa40e31e7fe9.tar.gz meta-impy-798d367d54a6c6379ad355bd8345fa40e31e7fe9.tar.bz2 meta-impy-798d367d54a6c6379ad355bd8345fa40e31e7fe9.tar.xz |
Second Life viewer sources 1.21.0-RC
Diffstat (limited to 'linden/indra/llcommon/llstring.cpp')
-rw-r--r-- | linden/indra/llcommon/llstring.cpp | 292 |
1 files changed, 55 insertions, 237 deletions
diff --git a/linden/indra/llcommon/llstring.cpp b/linden/indra/llcommon/llstring.cpp index a1f19ad..6a8efa8 100644 --- a/linden/indra/llcommon/llstring.cpp +++ b/linden/indra/llcommon/llstring.cpp | |||
@@ -1,6 +1,6 @@ | |||
1 | /** | 1 | /** |
2 | * @file llstring.cpp | 2 | * @file llstring.cpp |
3 | * @brief String utility functions and the LLString class. | 3 | * @brief String utility functions and the std::string class. |
4 | * | 4 | * |
5 | * $LicenseInfo:firstyear=2001&license=viewergpl$ | 5 | * $LicenseInfo:firstyear=2001&license=viewergpl$ |
6 | * | 6 | * |
@@ -47,6 +47,12 @@ std::string ll_safe_string(const char* in) | |||
47 | return std::string(); | 47 | return std::string(); |
48 | } | 48 | } |
49 | 49 | ||
50 | std::string ll_safe_string(const char* in, S32 maxlen) | ||
51 | { | ||
52 | if(in) return std::string(in, maxlen); | ||
53 | return std::string(); | ||
54 | } | ||
55 | |||
50 | U8 hex_as_nybble(char hex) | 56 | U8 hex_as_nybble(char hex) |
51 | { | 57 | { |
52 | if((hex >= '0') && (hex <= '9')) | 58 | if((hex >= '0') && (hex <= '9')) |
@@ -65,12 +71,12 @@ U8 hex_as_nybble(char hex) | |||
65 | } | 71 | } |
66 | 72 | ||
67 | 73 | ||
68 | bool _read_file_into_string(std::string& str, const char* filename) | 74 | bool _read_file_into_string(std::string& str, const std::string& filename) |
69 | { | 75 | { |
70 | llifstream ifs(filename, llifstream::binary); | 76 | llifstream ifs(filename, llifstream::binary); |
71 | if (!ifs.is_open()) | 77 | if (!ifs.is_open()) |
72 | { | 78 | { |
73 | llinfos << "Unable to open file" << filename << llendl; | 79 | llinfos << "Unable to open file " << filename << llendl; |
74 | return false; | 80 | return false; |
75 | } | 81 | } |
76 | 82 | ||
@@ -174,20 +180,6 @@ S32 utf16chars_to_wchar(const U16* inchars, llwchar* outchar) | |||
174 | return inchars - base; | 180 | return inchars - base; |
175 | } | 181 | } |
176 | 182 | ||
177 | S32 utf16chars_to_utf8chars(const U16* inchars, char* outchars, S32* nchars8p) | ||
178 | { | ||
179 | // Get 32 bit char32 | ||
180 | llwchar char32; | ||
181 | S32 nchars16 = utf16chars_to_wchar(inchars, &char32); | ||
182 | // Convert to utf8 | ||
183 | S32 nchars8 = wchar_to_utf8chars(char32, outchars); | ||
184 | if (nchars8p) | ||
185 | { | ||
186 | *nchars8p = nchars8; | ||
187 | } | ||
188 | return nchars16; | ||
189 | } | ||
190 | |||
191 | llutf16string wstring_to_utf16str(const LLWString &utf32str, S32 len) | 183 | llutf16string wstring_to_utf16str(const LLWString &utf32str, S32 len) |
192 | { | 184 | { |
193 | llutf16string out; | 185 | llutf16string out; |
@@ -216,7 +208,7 @@ llutf16string wstring_to_utf16str(const LLWString &utf32str) | |||
216 | return wstring_to_utf16str(utf32str, len); | 208 | return wstring_to_utf16str(utf32str, len); |
217 | } | 209 | } |
218 | 210 | ||
219 | llutf16string utf8str_to_utf16str ( const LLString& utf8str ) | 211 | llutf16string utf8str_to_utf16str ( const std::string& utf8str ) |
220 | { | 212 | { |
221 | LLWString wstr = utf8str_to_wstring ( utf8str ); | 213 | LLWString wstr = utf8str_to_wstring ( utf8str ); |
222 | return wstring_to_utf16str ( wstr ); | 214 | return wstring_to_utf16str ( wstr ); |
@@ -492,210 +484,10 @@ std::string utf16str_to_utf8str(const llutf16string& utf16str, S32 len) | |||
492 | return wstring_to_utf8str(utf16str_to_wstring(utf16str, len), len); | 484 | return wstring_to_utf8str(utf16str_to_wstring(utf16str, len), len); |
493 | } | 485 | } |
494 | 486 | ||
495 | |||
496 | //LLWString wstring_truncate(const LLWString &wstr, const S32 max_len) | ||
497 | //{ | ||
498 | // return wstr.substr(0, llmin((S32)wstr.length(), max_len)); | ||
499 | //} | ||
500 | // | ||
501 | // | ||
502 | //LLWString wstring_trim(const LLWString &wstr) | ||
503 | //{ | ||
504 | // LLWString outstr; | ||
505 | // outstr = wstring_trimhead(wstr); | ||
506 | // outstr = wstring_trimtail(outstr); | ||
507 | // return outstr; | ||
508 | //} | ||
509 | // | ||
510 | // | ||
511 | //LLWString wstring_trimhead(const LLWString &wstr) | ||
512 | //{ | ||
513 | // if(wstr.empty()) | ||
514 | // { | ||
515 | // return wstr; | ||
516 | // } | ||
517 | // | ||
518 | // S32 i = 0; | ||
519 | // while((i < (S32)wstr.length()) && iswspace(wstr[i])) | ||
520 | // { | ||
521 | // i++; | ||
522 | // } | ||
523 | // return wstr.substr(i, wstr.length() - i); | ||
524 | //} | ||
525 | // | ||
526 | // | ||
527 | //LLWString wstring_trimtail(const LLWString &wstr) | ||
528 | //{ | ||
529 | // if(wstr.empty()) | ||
530 | // { | ||
531 | // return wstr; | ||
532 | // } | ||
533 | // | ||
534 | // S32 len = (S32)wstr.length(); | ||
535 | // | ||
536 | // S32 i = len - 1; | ||
537 | // while (i >= 0 && iswspace(wstr[i])) | ||
538 | // { | ||
539 | // i--; | ||
540 | // } | ||
541 | // | ||
542 | // if (i >= 0) | ||
543 | // { | ||
544 | // return wstr.substr(0, i + 1); | ||
545 | // } | ||
546 | // return wstr; | ||
547 | //} | ||
548 | // | ||
549 | // | ||
550 | //LLWString wstring_copyinto(const LLWString &dest, const LLWString &src, const S32 insert_offset) | ||
551 | //{ | ||
552 | // llassert( insert_offset <= (S32)dest.length() ); | ||
553 | // | ||
554 | // LLWString out_str = dest.substr(0, insert_offset); | ||
555 | // out_str += src; | ||
556 | // LLWString tail = dest.substr(insert_offset); | ||
557 | // out_str += tail; | ||
558 | // | ||
559 | // return out_str; | ||
560 | //} | ||
561 | |||
562 | |||
563 | //LLWString wstring_detabify(const LLWString &wstr, const S32 num_spaces) | ||
564 | //{ | ||
565 | // LLWString out_str; | ||
566 | // // Replace tabs with spaces | ||
567 | // for (S32 i = 0; i < (S32)wstr.length(); i++) | ||
568 | // { | ||
569 | // if (wstr[i] == '\t') | ||
570 | // { | ||
571 | // for (S32 j = 0; j < num_spaces; j++) | ||
572 | // out_str += ' '; | ||
573 | // } | ||
574 | // else | ||
575 | // { | ||
576 | // out_str += wstr[i]; | ||
577 | // } | ||
578 | // } | ||
579 | // return out_str; | ||
580 | //} | ||
581 | |||
582 | |||
583 | //LLWString wstring_makeASCII(const LLWString &wstr) | ||
584 | //{ | ||
585 | // // Replace non-ASCII chars with replace_char | ||
586 | // LLWString out_str = wstr; | ||
587 | // for (S32 i = 0; i < (S32)out_str.length(); i++) | ||
588 | // { | ||
589 | // if (out_str[i] > 0x7f) | ||
590 | // { | ||
591 | // out_str[i] = LL_UNKNOWN_CHAR; | ||
592 | // } | ||
593 | // } | ||
594 | // return out_str; | ||
595 | //} | ||
596 | |||
597 | |||
598 | //LLWString wstring_substChar(const LLWString &wstr, const llwchar target_char, const llwchar replace_char) | ||
599 | //{ | ||
600 | // // Replace all occurences of target_char with replace_char | ||
601 | // LLWString out_str = wstr; | ||
602 | // for (S32 i = 0; i < (S32)out_str.length(); i++) | ||
603 | // { | ||
604 | // if (out_str[i] == target_char) | ||
605 | // { | ||
606 | // out_str[i] = replace_char; | ||
607 | // } | ||
608 | // } | ||
609 | // return out_str; | ||
610 | //} | ||
611 | // | ||
612 | // | ||
613 | //LLWString wstring_tolower(const LLWString &wstr) | ||
614 | //{ | ||
615 | // LLWString out_str = wstr; | ||
616 | // for (S32 i = 0; i < (S32)out_str.length(); i++) | ||
617 | // { | ||
618 | // out_str[i] = towlower(out_str[i]); | ||
619 | // } | ||
620 | // return out_str; | ||
621 | //} | ||
622 | // | ||
623 | // | ||
624 | //LLWString wstring_convert_to_lf(const LLWString &wstr) | ||
625 | //{ | ||
626 | // const llwchar CR = 13; | ||
627 | // // Remove carriage returns from string with CRLF | ||
628 | // LLWString out_str; | ||
629 | // | ||
630 | // for (S32 i = 0; i < (S32)wstr.length(); i++) | ||
631 | // { | ||
632 | // if (wstr[i] != CR) | ||
633 | // { | ||
634 | // out_str += wstr[i]; | ||
635 | // } | ||
636 | // } | ||
637 | // return out_str; | ||
638 | //} | ||
639 | // | ||
640 | // | ||
641 | //LLWString wstring_convert_to_crlf(const LLWString &wstr) | ||
642 | //{ | ||
643 | // const llwchar LF = 10; | ||
644 | // const llwchar CR = 13; | ||
645 | // // Remove carriage returns from string with CRLF | ||
646 | // LLWString out_str; | ||
647 | // | ||
648 | // for (S32 i = 0; i < (S32)wstr.length(); i++) | ||
649 | // { | ||
650 | // if (wstr[i] == LF) | ||
651 | // { | ||
652 | // out_str += CR; | ||
653 | // } | ||
654 | // out_str += wstr[i]; | ||
655 | // } | ||
656 | // return out_str; | ||
657 | //} | ||
658 | |||
659 | |||
660 | //S32 wstring_compare_insensitive(const LLWString &lhs, const LLWString &rhs) | ||
661 | //{ | ||
662 | // | ||
663 | // if (lhs == rhs) | ||
664 | // { | ||
665 | // return 0; | ||
666 | // } | ||
667 | // | ||
668 | // if (lhs.empty()) | ||
669 | // { | ||
670 | // return rhs.empty() ? 0 : 1; | ||
671 | // } | ||
672 | // | ||
673 | // if (rhs.empty()) | ||
674 | // { | ||
675 | // return -1; | ||
676 | // } | ||
677 | // | ||
678 | //#ifdef LL_LINUX | ||
679 | // // doesn't work because gcc 2.95 doesn't correctly implement c_str(). Sigh... | ||
680 | // llerrs << "wstring_compare_insensitive doesn't work on Linux!" << llendl; | ||
681 | // return 0; | ||
682 | //#else | ||
683 | // LLWString lhs_lower = lhs; | ||
684 | // LLWString::toLower(lhs_lower); | ||
685 | // std::string lhs_lower = wstring_to_utf8str(lhs_lower); | ||
686 | // LLWString rhs_lower = lhs; | ||
687 | // LLWString::toLower(rhs_lower); | ||
688 | // std::string rhs_lower = wstring_to_utf8str(rhs_lower); | ||
689 | // | ||
690 | // return strcmp(lhs_lower.c_str(), rhs_lower.c_str()); | ||
691 | //#endif | ||
692 | //} | ||
693 | |||
694 | |||
695 | std::string utf8str_trim(const std::string& utf8str) | 487 | std::string utf8str_trim(const std::string& utf8str) |
696 | { | 488 | { |
697 | LLWString wstr = utf8str_to_wstring(utf8str); | 489 | LLWString wstr = utf8str_to_wstring(utf8str); |
698 | LLWString::trim(wstr); | 490 | LLWStringUtil::trim(wstr); |
699 | return wstring_to_utf8str(wstr); | 491 | return wstring_to_utf8str(wstr); |
700 | } | 492 | } |
701 | 493 | ||
@@ -703,7 +495,7 @@ std::string utf8str_trim(const std::string& utf8str) | |||
703 | std::string utf8str_tolower(const std::string& utf8str) | 495 | std::string utf8str_tolower(const std::string& utf8str) |
704 | { | 496 | { |
705 | LLWString out_str = utf8str_to_wstring(utf8str); | 497 | LLWString out_str = utf8str_to_wstring(utf8str); |
706 | LLWString::toLower(out_str); | 498 | LLWStringUtil::toLower(out_str); |
707 | return wstring_to_utf8str(out_str); | 499 | return wstring_to_utf8str(out_str); |
708 | } | 500 | } |
709 | 501 | ||
@@ -712,7 +504,7 @@ S32 utf8str_compare_insensitive(const std::string& lhs, const std::string& rhs) | |||
712 | { | 504 | { |
713 | LLWString wlhs = utf8str_to_wstring(lhs); | 505 | LLWString wlhs = utf8str_to_wstring(lhs); |
714 | LLWString wrhs = utf8str_to_wstring(rhs); | 506 | LLWString wrhs = utf8str_to_wstring(rhs); |
715 | return LLWString::compareInsensitive(wlhs.c_str(), wrhs.c_str()); | 507 | return LLWStringUtil::compareInsensitive(wlhs, wrhs); |
716 | } | 508 | } |
717 | 509 | ||
718 | std::string utf8str_truncate(const std::string& utf8str, const S32 max_len) | 510 | std::string utf8str_truncate(const std::string& utf8str, const S32 max_len) |
@@ -756,7 +548,7 @@ std::string utf8str_substChar( | |||
756 | const llwchar replace_char) | 548 | const llwchar replace_char) |
757 | { | 549 | { |
758 | LLWString wstr = utf8str_to_wstring(utf8str); | 550 | LLWString wstr = utf8str_to_wstring(utf8str); |
759 | LLWString::replaceChar(wstr, target_char, replace_char); | 551 | LLWStringUtil::replaceChar(wstr, target_char, replace_char); |
760 | //wstr = wstring_substChar(wstr, target_char, replace_char); | 552 | //wstr = wstring_substChar(wstr, target_char, replace_char); |
761 | return wstring_to_utf8str(wstr); | 553 | return wstring_to_utf8str(wstr); |
762 | } | 554 | } |
@@ -764,7 +556,7 @@ std::string utf8str_substChar( | |||
764 | std::string utf8str_makeASCII(const std::string& utf8str) | 556 | std::string utf8str_makeASCII(const std::string& utf8str) |
765 | { | 557 | { |
766 | LLWString wstr = utf8str_to_wstring(utf8str); | 558 | LLWString wstr = utf8str_to_wstring(utf8str); |
767 | LLWString::_makeASCII(wstr); | 559 | LLWStringUtil::_makeASCII(wstr); |
768 | return wstring_to_utf8str(wstr); | 560 | return wstring_to_utf8str(wstr); |
769 | } | 561 | } |
770 | 562 | ||
@@ -929,6 +721,32 @@ namespace LLStringFn | |||
929 | } | 721 | } |
930 | } | 722 | } |
931 | } | 723 | } |
724 | |||
725 | // https://wiki.lindenlab.com/wiki/Unicode_Guidelines has details on | ||
726 | // allowable code points for XML. Specifically, they are: | ||
727 | // 0x09, 0x0a, 0x0d, and 0x20 on up. JC | ||
728 | std::string strip_invalid_xml(const std::string& input) | ||
729 | { | ||
730 | std::string output; | ||
731 | output.reserve( input.size() ); | ||
732 | std::string::const_iterator it = input.begin(); | ||
733 | while (it != input.end()) | ||
734 | { | ||
735 | // Must compare as unsigned for >= | ||
736 | // Test most likely match first | ||
737 | const unsigned char c = (unsigned char)*it; | ||
738 | if ( c >= (unsigned char)0x20 // SPACE | ||
739 | || c == (unsigned char)0x09 // TAB | ||
740 | || c == (unsigned char)0x0a // LINE_FEED | ||
741 | || c == (unsigned char)0x0d ) // CARRIAGE_RETURN | ||
742 | { | ||
743 | output.push_back(c); | ||
744 | } | ||
745 | ++it; | ||
746 | } | ||
747 | return output; | ||
748 | } | ||
749 | |||
932 | } | 750 | } |
933 | 751 | ||
934 | 752 | ||
@@ -938,19 +756,19 @@ namespace LLStringFn | |||
938 | #ifdef _DEBUG | 756 | #ifdef _DEBUG |
939 | 757 | ||
940 | template<class T> | 758 | template<class T> |
941 | void LLStringBase<T>::testHarness() | 759 | void LLStringUtilBase<T>::testHarness() |
942 | { | 760 | { |
943 | LLString s1; | 761 | std::string s1; |
944 | 762 | ||
945 | llassert( s1.c_str() == NULL ); | 763 | llassert( s1.c_str() == NULL ); |
946 | llassert( s1.size() == 0 ); | 764 | llassert( s1.size() == 0 ); |
947 | llassert( s1.empty() ); | 765 | llassert( s1.empty() ); |
948 | 766 | ||
949 | LLString s2( "hello"); | 767 | std::string s2( "hello"); |
950 | llassert( !strcmp( s2.c_str(), "hello" ) ); | 768 | llassert( !strcmp( s2.c_str(), "hello" ) ); |
951 | llassert( s2.size() == 5 ); | 769 | llassert( s2.size() == 5 ); |
952 | llassert( !s2.empty() ); | 770 | llassert( !s2.empty() ); |
953 | LLString s3( s2 ); | 771 | std::string s3( s2 ); |
954 | 772 | ||
955 | llassert( "hello" == s2 ); | 773 | llassert( "hello" == s2 ); |
956 | llassert( s2 == "hello" ); | 774 | llassert( s2 == "hello" ); |
@@ -959,12 +777,12 @@ void LLStringBase<T>::testHarness() | |||
959 | llassert( "gello" != s2 ); | 777 | llassert( "gello" != s2 ); |
960 | llassert( s2 != "gello" ); | 778 | llassert( s2 != "gello" ); |
961 | 779 | ||
962 | LLString s4 = s2; | 780 | std::string s4 = s2; |
963 | llassert( !s4.empty() ); | 781 | llassert( !s4.empty() ); |
964 | s4.empty(); | 782 | s4.empty(); |
965 | llassert( s4.empty() ); | 783 | llassert( s4.empty() ); |
966 | 784 | ||
967 | LLString s5(""); | 785 | std::string s5(""); |
968 | llassert( s5.empty() ); | 786 | llassert( s5.empty() ); |
969 | 787 | ||
970 | llassert( isValidIndex(s5, 0) ); | 788 | llassert( isValidIndex(s5, 0) ); |
@@ -978,8 +796,8 @@ void LLStringBase<T>::testHarness() | |||
978 | llassert( s4 == "hello again!hello again!" ); | 796 | llassert( s4 == "hello again!hello again!" ); |
979 | 797 | ||
980 | 798 | ||
981 | LLString s6 = s2 + " " + s2; | 799 | std::string s6 = s2 + " " + s2; |
982 | LLString s7 = s6; | 800 | std::string s7 = s6; |
983 | llassert( s6 == s7 ); | 801 | llassert( s6 == s7 ); |
984 | llassert( !( s6 != s7) ); | 802 | llassert( !( s6 != s7) ); |
985 | llassert( !(s6 < s7) ); | 803 | llassert( !(s6 < s7) ); |
@@ -1002,10 +820,10 @@ void LLStringBase<T>::testHarness() | |||
1002 | s2.insert( 1, "awn, don't yel"); | 820 | s2.insert( 1, "awn, don't yel"); |
1003 | llassert( s2 == "yawn, don't yell"); | 821 | llassert( s2 == "yawn, don't yell"); |
1004 | 822 | ||
1005 | LLString s8 = s2.substr( 6, 5 ); | 823 | std::string s8 = s2.substr( 6, 5 ); |
1006 | llassert( s8 == "don't" ); | 824 | llassert( s8 == "don't" ); |
1007 | 825 | ||
1008 | LLString s9 = " \t\ntest \t\t\n "; | 826 | std::string s9 = " \t\ntest \t\t\n "; |
1009 | trim(s9); | 827 | trim(s9); |
1010 | llassert( s9 == "test" ); | 828 | llassert( s9 == "test" ); |
1011 | 829 | ||
@@ -1020,17 +838,17 @@ void LLStringBase<T>::testHarness() | |||
1020 | llassert( s9 == "abc123&*(abc" ); | 838 | llassert( s9 == "abc123&*(abc" ); |
1021 | 839 | ||
1022 | 840 | ||
1023 | LLString s10( 10, 'x' ); | 841 | std::string s10( 10, 'x' ); |
1024 | llassert( s10 == "xxxxxxxxxx" ); | 842 | llassert( s10 == "xxxxxxxxxx" ); |
1025 | 843 | ||
1026 | LLString s11( "monkey in the middle", 7, 2 ); | 844 | std::string s11( "monkey in the middle", 7, 2 ); |
1027 | llassert( s11 == "in" ); | 845 | llassert( s11 == "in" ); |
1028 | 846 | ||
1029 | LLString s12; //empty | 847 | std::string s12; //empty |
1030 | s12 += "foo"; | 848 | s12 += "foo"; |
1031 | llassert( s12 == "foo" ); | 849 | llassert( s12 == "foo" ); |
1032 | 850 | ||
1033 | LLString s13; //empty | 851 | std::string s13; //empty |
1034 | s13 += 'f'; | 852 | s13 += 'f'; |
1035 | llassert( s13 == "f" ); | 853 | llassert( s13 == "f" ); |
1036 | } | 854 | } |