📄 strtst.cpp
字号:
a.assign(5, 'a'); prstr(a, "aaaaa [0]"); String null2 = ""; compare(null1.data(), null2.data(), true); null2 = ""; compare(null1.data(), null2.data(), true); a = null1; compare(null1.data(), a.data(), true); a = "abc"; a.erase(); compare(null2.data(), a.data(), true); cout << "test substring\n"; cout << " ... doing test " << (Totals.Value() + 1 ) << "\n"; prstr(w.substr(2,5), "23456 [0]"); prstr(w.substr(4), "456789 [0]"); prstr(w.substr(), "0123456789 [0]"); char buff[20]; int rlen = w.copy(buff,5,2); prstr(String(buff,rlen), "23456 [0]"); rlen = w.copy(buff,6); prstr(String(buff,rlen), "012345 [0]"); cout << "test insert\n"; cout << " ... doing test " << (Totals.Value() + 1 ) << "\n"; x = "0123456789"; a = "abcdefg"; x.insert(3, a, 2, 4); prstr(x, "012cdef3456789 [0]"); x = "0123456789"; x.insert(4, a, 2, String::npos); prstr(x, "0123cdefg456789 [0]"); x = "0123456789"; x.insert(4, a); prstr(x, "0123abcdefg456789 [0]"); x = "0123456789"; x.insert(4, "abcdefg", 2); prstr(x, "0123ab456789 [0]"); x = "0123456789"; x.insert(4, "abcdefg"); prstr(x, "0123abcdefg456789 [0]"); x = "0123456789"; x.insert(4, 5, 'a'); prstr(x, "0123aaaaa456789 [0]"); cout << "test erase\n"; cout << " ... doing test " << (Totals.Value() + 1 ) << "\n"; x = "0123456789"; x.erase(3, 4); prstr(x, "012789 [0]"); x = "0123456789"; x.erase(3, 40); prstr(x, "012 [0]"); x = "0123456789"; x.erase(3); prstr(x, "012 [0]"); x = "0123456789"; x.erase(0, 4); prstr(x, "456789 [0]"); x = "0123456789"; x.erase(); prstr(x, " [0]"); x = "abcdef"; x.clear(); prstr(x, " [0]"); cout << "test replace\n"; cout << " ... doing test " << (Totals.Value() + 1 ) << "\n"; x = "0123456789"; x.replace(3, 2, a, 1, 4); prstr(x, "012bcde56789 [0]"); x = "0123456789"; x.replace(3, 2, a, 1, String::npos); prstr(x, "012bcdefg56789 [0]"); x = "0123456789"; x.replace(3, 2, a); prstr(x, "012abcdefg56789 [0]"); x = "0123456789"; x.replace(3, 20, a, 1, 4); prstr(x, "012bcde [0]"); x = "0123456789"; x.replace(3, 20, a, 1, String::npos); prstr(x, "012bcdefg [0]"); x = "0123456789"; x.replace(3, 2, a, 0, 2); prstr(x, "012ab56789 [0]"); x = "0123456789"; x.replace(3, 20, a); prstr(x, "012abcdefg [0]"); x = "0123456789"; x.replace(3, 20, "abcdefg"); prstr(x, "012abcdefg [0]"); x = "0123456789"; x.replace(3, 2, "ab"); prstr(x, "012ab56789 [0]"); x = "0123456789"; x.replace(3, 2, "abcdefg"); prstr(x, "012abcdefg56789 [0]"); x = "0123456789"; x.replace(3, 2, 2, 'a'); prstr(x, "012aa56789 [0]"); x = "0123456789"; x.replace(5, 10, 10, 'a'); prstr(x, "01234aaaaaaaaaa [0]"); x = "0123456789"; x.replace(5, 10, 12, 'a'); prstr(x, "01234aaaaaaaaaaaa [0]"); x = "0123456789"; x.replace(5, 10, 8, 'a'); prstr(x, "01234aaaaaaaa [0]"); { x = "0123456789"; String y = x; prstr(y, "0123456789 [1]"); x.replace(5,3,"pqr"); prstr(y, "0123456789 [0]"); prstr(x, "01234pqr89 [0]"); } cout << "test insert with capacity\n"; cout << " ... doing test " << (Totals.Value() + 1 ) << "\n"; x.reserve(100); const char* xd1 = x.data(); x.assign("0123456789"); a = "abcdefg"; x.insert(3, a, 2, 4); prstr(x, "012cdef3456789 [0]"); x.assign("0123456789"); x.insert(4, a, 2, String::npos); prstr(x, "0123cdefg456789 [0]"); x.assign("0123456789"); x.insert(4, a); prstr(x, "0123abcdefg456789 [0]"); x.assign("0123456789"); x.insert(4, "abcdefg", 2); prstr(x, "0123ab456789 [0]"); x.assign("0123456789"); x.insert(4, "abcdefg"); prstr(x, "0123abcdefg456789 [0]"); x.assign("0123456789"); x.insert(4, 5, 'a'); prstr(x, "0123aaaaa456789 [0]"); cout << "test erase with capacity\n"; cout << " ... doing test " << (Totals.Value() + 1 ) << "\n"; x.assign("0123456789"); x.erase(3, 4); prstr(x, "012789 [0]"); x.assign("0123456789"); x.erase(3, 40); prstr(x, "012 [0]"); x.assign("0123456789"); x.erase(3); prstr(x, "012 [0]"); x.assign("0123456789"); x.erase(0, 4); prstr(x, "456789 [0]"); x.assign("0123456789"); x.erase(); prstr(x, " [0]"); cout << "test replace with capacity\n"; cout << " ... doing test " << (Totals.Value() + 1 ) << "\n"; x.assign("0123456789"); x.replace(3, 2, a, 1, 4); prstr(x, "012bcde56789 [0]"); x.assign("0123456789"); x.replace(3, 2, a, 1, String::npos); prstr(x, "012bcdefg56789 [0]"); x.assign("0123456789"); x.replace(3, 2, a); prstr(x, "012abcdefg56789 [0]"); x.assign("0123456789"); x.replace(3, 20, a, 1, 4); prstr(x, "012bcde [0]"); x.assign("0123456789"); x.replace(3, 20, a, 1, String::npos); prstr(x, "012bcdefg [0]"); x.assign("0123456789"); x.replace(3, 2, a, 0, 2); prstr(x, "012ab56789 [0]"); x.assign("0123456789"); x.replace(3, 20, a); prstr(x, "012abcdefg [0]"); x.assign("0123456789"); x.replace(3, 20, "abcdefg"); prstr(x, "012abcdefg [0]"); x.assign("0123456789"); x.replace(3, 2, "ab"); prstr(x, "012ab56789 [0]"); x.assign("0123456789"); x.replace(3, 2, "abcdefg"); prstr(x, "012abcdefg56789 [0]"); x.assign("0123456789"); x.replace(3, 2, 2, 'a'); prstr(x, "012aa56789 [0]"); x.assign("0123456789"); x.replace(5, 10, 10, 'a'); prstr(x, "01234aaaaaaaaaa [0]"); x.assign("0123456789"); x.replace(5, 10, 12, 'a'); prstr(x, "01234aaaaaaaaaaaa [0]"); x.assign("0123456789"); x.replace(5, 10, 8, 'a'); prstr(x, "01234aaaaaaaa [0]"); compare(xd1, x.data()); compare(x.capacity(), 100); cout << "test some weirdos\n"; cout << " ... doing test " << (Totals.Value() + 1 ) << "\n"; a.erase(); a.reserve(10); ad1 = a.data(); a.assign("abcdefg"); compare(ad1, a.data()); b = a; prstr(a, "abcdefg [0]"); compare(a.capacity(), 10); a = a; b = a; prstr(a, "abcdefg [1]"); compare(a.capacity(), 7); a.erase(); a.reserve(10); ad1 = a.data(); a.assign("abcdefghij"); compare(ad1, a.data()); b = a; prstr(a, "abcdefghij [0]"); compare(a.capacity(), 10); a = a; b = a; prstr(a, "abcdefghij [1]"); compare(a.capacity(), 10); a.erase(); a.reserve(10); a.assign("abcdefghijk"); b = a; a = a; // check a is not reallocated prstr(a, "abcdefghijk [1]"); a.erase(); a.reserve(20); ad1 = a.data(); a.assign("abcdefghij"); a.replace(2,5,a); b = a; prstr(a, "ababcdefghijhij [0]"); compare(ad1, a.data()); a.erase(); a.reserve(20); ad1 = a.data(); a.assign("abcdefghij"); a.replace(2,5,a,7,3); b = a; prstr(a, "abhijhij [0]"); compare(ad1, a.data()); a.erase(); a.reserve(20); ad1 = a.data(); a.assign("abcdefghij"); a.insert(2,a); b = a; prstr(a, "ababcdefghijcdefghij [0]"); compare(ad1, a.data()); a.erase(); a.reserve(20); ad1 = a.data(); a.assign("abcdefghij"); a.insert(2,a,7,3); b = a; prstr(a, "abhijcdefghij [0]"); compare(ad1, a.data()); a.erase(); a.reserve(20); a = "abcdefghij"; compare(a.capacity(), 10); a.replace(2,5,a); b = a; prstr(a, "ababcdefghijhij [1]"); a.erase(); a.reserve(20); a = "abcdefghij"; compare(a.capacity(), 10); a.replace(2,5,a,7,3); b = a; prstr(a, "abhijhij [1]"); a.erase(); a.reserve(20); a = "abcdefghij"; a.insert(2,a); b = a; prstr(a, "ababcdefghijcdefghij [1]"); a.erase(); a.reserve(20); a = "abcdefghij"; a.insert(2,a,7,3); b = a; prstr(a, "abhijcdefghij [1]"); { String A = "0123456789"; A.reserve(1); // will set capacity to A.size() = 10 const char* d = A.data(); A.erase(1,9); String B = A; prstr(A, "0 [0]"); prstr(B, "0 [0]"); compare(d,A.data(),true); } { String A = "0123456789"; const char* d = A.data(); compare(d,A.data(),true); A.erase(1,9); String B = A; prstr(A, "0 [1]"); prstr(B, "0 [1]"); } { String A = "0123456789"; const char* d = A.data(); A.reserve(1); // will set capacity to A.size() = 10 compare(d,A.data(),true); A.erase(1,9); String B = A; prstr(A, "0 [0]"); prstr(B, "0 [0]"); compare(d,A.data(),true); } { String A = "01234"; A += "56789"; A.reserve(1); // will set capacity to A.size() = 10 prstr(A, "0123456789 [0]"); A.erase(1,9); prstr(A, "0 [0]"); } { String A = "0123456789"; String B = A; prstr(A, "0123456789 [1]"); prstr(B, "0123456789 [1]"); A.reserve(1); // will set capacity to A.size() = 10 prstr(A, "0123456789 [0]"); prstr(B, "0123456789 [0]"); A.erase(1,9); B = A; prstr(A, "0 [0]"); prstr(B, "0 [0]"); } { String A = "0123456789"; const char* c = A.c_str(); compare(c,A.data(),true); A.reserve(1); // will set capacity to A.size() = 10 compare(c,A.data(),false); prstr(A, "0123456789 [0]"); A.erase(1,9); prstr(A, "0 [0]"); } { // check transfer from function and avoidance of copy String A; String* B; A = alphabet1(B); // string initiated as CharRepeated prstr(A, "ABCDEFGHIJKLMNOPQRSTUVWXYZ [1]"); prstr(*B, "ABCDEFGHIJKLMNOPQRSTUVWXYZ [1]"); delete B; B = 0; prstr(A, "ABCDEFGHIJKLMNOPQRSTUVWXYZ [0]"); A = alphabet2(B); // string initiated as character string prstr(A, "ABCDEFGHIJKLMNOPQRSTUVWXYZ [1]"); prstr(*B, "ABCDEFGHIJKLMNOPQRSTUVWXYZ [1]"); delete B; B = 0; prstr(A, "ABCDEFGHIJKLMNOPQRSTUVWXYZ [0]"); } cout << "test comparisons\n"; cout << " ... doing test " << (Totals.Value() + 1 ) << "\n"; a = "012345a"; b = "012345b"; compare_bool((a == b), false); compare_bool((a != b), true); compare_bool((a > b), false); compare_bool((a < b), true); compare_bool((a >= b), false); compare_bool((a <= b), true); compare_bool((b == a), false); compare_bool((b != a), true); compare_bool((b < a), false); compare_bool((b > a), true); compare_bool((b <= a), false); compare_bool((b >= a), true); a = "012345"; b = "012345b"; compare_bool((a == b), false); compare_bool((a != b), true); compare_bool((a > b), false); compare_bool((a < b), true); compare_bool((a >= b), false); compare_bool((a <= b), true); compare_bool((b == a), false); compare_bool((b != a), true); compare_bool((b < a), false); compare_bool((b > a), true); compare_bool((b <= a), false); compare_bool((b >= a), true); a = "012345"; b = "012345"; compare_bool((a == b), true);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -