diff options
-rw-r--r-- | test/unit/testCellBuffer.cxx | 137 | ||||
-rw-r--r-- | test/unit/testDocument.cxx | 100 | ||||
-rw-r--r-- | test/unit/testUniConversion.cxx | 4 |
3 files changed, 122 insertions, 119 deletions
diff --git a/test/unit/testCellBuffer.cxx b/test/unit/testCellBuffer.cxx index b44f47735..eaa5b0f31 100644 --- a/test/unit/testCellBuffer.cxx +++ b/test/unit/testCellBuffer.cxx @@ -30,20 +30,23 @@ using namespace Scintilla; using namespace Scintilla::Internal; // Test CellBuffer. +bool Equal(const char *ptr, std::string_view sv) noexcept { + return memcmp(ptr, sv.data(), sv.length()) == 0; +} TEST_CASE("CellBuffer") { - const char sText[] = "Scintilla"; - const Sci::Position sLength = static_cast<Sci::Position>(strlen(sText)); + constexpr std::string_view sText = "Scintilla"; + constexpr Sci::Position sLength = sText.length(); CellBuffer cb(true, false); SECTION("InsertOneLine") { bool startSequence = false; - const char *cpChange = cb.InsertString(0, sText, sLength, startSequence); + const char *cpChange = cb.InsertString(0, sText.data(), sLength, startSequence); REQUIRE(startSequence); REQUIRE(sLength == cb.Length()); - REQUIRE(memcmp(cpChange, sText, sLength) == 0); + REQUIRE(Equal(cpChange, sText)); REQUIRE(1 == cb.Lines()); REQUIRE(0 == cb.LineStart(0)); REQUIRE(0 == cb.LineFromPosition(0)); @@ -54,13 +57,13 @@ TEST_CASE("CellBuffer") { } SECTION("InsertTwoLines") { - const char sText2[] = "Two\nLines"; - const Sci::Position sLength2 = static_cast<Sci::Position>(strlen(sText2)); + constexpr std::string_view sText2 = "Two\nLines"; + constexpr Sci::Position sLength2 = sText2.length(); bool startSequence = false; - const char *cpChange = cb.InsertString(0, sText2, sLength2, startSequence); + const char *cpChange = cb.InsertString(0, sText2.data(), sLength2, startSequence); REQUIRE(startSequence); REQUIRE(sLength2 == cb.Length()); - REQUIRE(memcmp(cpChange, sText2, sLength2) == 0); + REQUIRE(Equal(cpChange, sText2)); REQUIRE(2 == cb.Lines()); REQUIRE(0 == cb.LineStart(0)); REQUIRE(0 == cb.LineFromPosition(0)); @@ -78,45 +81,45 @@ TEST_CASE("CellBuffer") { bool startSequence = false; { // Unix \n - const char sText2[] = "Two\nLines"; - const Sci::Position sLength2 = static_cast<Sci::Position>(strlen(sText2)); - cb.InsertString(0, sText2, strlen(sText2), startSequence); + constexpr std::string_view sText2 = "Two\nLines"; + constexpr Sci::Position sLength2 = sText2.length(); + cb.InsertString(0, sText2.data(), sLength2, startSequence); REQUIRE(3 == cb.LineEnd(0)); REQUIRE(sLength2 == cb.LineEnd(1)); cb.DeleteChars(0, sLength2, startSequence); } { // Windows \r\n - const char sText2[] = "Two\r\nLines"; - const Sci::Position sLength2 = static_cast<Sci::Position>(strlen(sText2)); - cb.InsertString(0, sText2, sLength2, startSequence); + constexpr std::string_view sText2 = "Two\r\nLines"; + constexpr Sci::Position sLength2 = sText2.length(); + cb.InsertString(0, sText2.data(), sLength2, startSequence); REQUIRE(3 == cb.LineEnd(0)); REQUIRE(sLength2 == cb.LineEnd(1)); cb.DeleteChars(0, sLength2, startSequence); } { // Old macOS \r - const char sText2[] = "Two\rLines"; - const Sci::Position sLength2 = static_cast<Sci::Position>(strlen(sText2)); - cb.InsertString(0, sText2, strlen(sText2), startSequence); + constexpr std::string_view sText2 = "Two\rLines"; + constexpr Sci::Position sLength2 = sText2.length(); + cb.InsertString(0, sText2.data(), sLength2, startSequence); REQUIRE(3 == cb.LineEnd(0)); REQUIRE(sLength2 == cb.LineEnd(1)); cb.DeleteChars(0, sLength2, startSequence); } { // Unicode NEL is U+0085 \xc2\x85 - const char sText2[] = "Two\xc2\x85Lines"; - const Sci::Position sLength2 = static_cast<Sci::Position>(strlen(sText2)); - cb.InsertString(0, sText2, sLength2, startSequence); + constexpr std::string_view sText2 = "Two\xc2\x85Lines"; + constexpr Sci::Position sLength2 = sText2.length(); + cb.InsertString(0, sText2.data(), sLength2, startSequence); REQUIRE(3 == cb.LineEnd(0)); REQUIRE(sLength2 == cb.LineEnd(1)); cb.DeleteChars(0, sLength2, startSequence); } { // Unicode LS line separator is U+2028 \xe2\x80\xa8 - const char sText2[] = "Two\xe2\x80\xa8Lines"; - const Sci::Position sLength2 = static_cast<Sci::Position>(strlen(sText2)); - cb.InsertString(0, sText2, sLength2, startSequence); + constexpr std::string_view sText2 = "Two\xe2\x80\xa8Lines"; + constexpr Sci::Position sLength2 = sText2.length(); + cb.InsertString(0, sText2.data(), sLength2, startSequence); REQUIRE(3 == cb.LineEnd(0)); REQUIRE(sLength2 == cb.LineEnd(1)); cb.DeleteChars(0, sLength2, startSequence); @@ -129,36 +132,36 @@ TEST_CASE("CellBuffer") { cb.SetUndoCollection(false); REQUIRE(!cb.IsCollectingUndo()); bool startSequence = false; - const char *cpChange = cb.InsertString(0, sText, sLength, startSequence); + const char *cpChange = cb.InsertString(0, sText.data(), sLength, startSequence); REQUIRE(!startSequence); REQUIRE(sLength == cb.Length()); - REQUIRE(memcmp(cpChange, sText, sLength) == 0); + REQUIRE(Equal(cpChange, sText)); REQUIRE(!cb.CanUndo()); REQUIRE(!cb.CanRedo()); } SECTION("UndoRedo") { - const char sTextDeleted[] = "ci"; - const char sTextAfterDeletion[] = "Sntilla"; + constexpr std::string_view sTextDeleted = "ci"; + constexpr std::string_view sTextAfterDeletion = "Sntilla"; bool startSequence = false; - const char *cpChange = cb.InsertString(0, sText, sLength, startSequence); + const char *cpChange = cb.InsertString(0, sText.data(), sLength, startSequence); REQUIRE(startSequence); REQUIRE(sLength == cb.Length()); - REQUIRE(memcmp(cpChange, sText, sLength) == 0); - REQUIRE(memcmp(cb.BufferPointer(), sText, sLength) == 0); + REQUIRE(Equal(cpChange, sText)); + REQUIRE(Equal(cb.BufferPointer(), sText)); REQUIRE(cb.CanUndo()); REQUIRE(!cb.CanRedo()); const char *cpDeletion = cb.DeleteChars(1, 2, startSequence); REQUIRE(startSequence); - REQUIRE(memcmp(cpDeletion, sTextDeleted, strlen(sTextDeleted)) == 0); - REQUIRE(memcmp(cb.BufferPointer(), sTextAfterDeletion, strlen(sTextAfterDeletion)) == 0); + REQUIRE(Equal(cpDeletion, sTextDeleted)); + REQUIRE(Equal(cb.BufferPointer(), sTextAfterDeletion)); REQUIRE(cb.CanUndo()); REQUIRE(!cb.CanRedo()); int steps = cb.StartUndo(); REQUIRE(steps == 1); cb.PerformUndoStep(); - REQUIRE(memcmp(cb.BufferPointer(), sText, sLength) == 0); + REQUIRE(Equal(cb.BufferPointer(), sText)); REQUIRE(cb.CanUndo()); REQUIRE(cb.CanRedo()); @@ -172,14 +175,14 @@ TEST_CASE("CellBuffer") { steps = cb.StartRedo(); REQUIRE(steps == 1); cb.PerformRedoStep(); - REQUIRE(memcmp(cb.BufferPointer(), sText, sLength) == 0); + REQUIRE(Equal(cb.BufferPointer(), sText)); REQUIRE(cb.CanUndo()); REQUIRE(cb.CanRedo()); steps = cb.StartRedo(); REQUIRE(steps == 1); cb.PerformRedoStep(); - REQUIRE(memcmp(cb.BufferPointer(), sTextAfterDeletion, strlen(sTextAfterDeletion)) == 0); + REQUIRE(Equal(cb.BufferPointer(), sTextAfterDeletion)); REQUIRE(cb.CanUndo()); REQUIRE(!cb.CanRedo()); @@ -201,7 +204,7 @@ TEST_CASE("CellBuffer") { cb.SetReadOnly(true); REQUIRE(cb.IsReadOnly()); bool startSequence = false; - cb.InsertString(0, sText, sLength, startSequence); + cb.InsertString(0, sText.data(), sLength, startSequence); REQUIRE(cb.Length() == 0); } @@ -239,8 +242,8 @@ TEST_CASE("CharacterIndex") { REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf32) == 0); REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf32) == 1); - const char *hwair = "\xF0\x90\x8D\x88"; - cb.InsertString(0, hwair, strlen(hwair), startSequence); + constexpr std::string_view hwair = "\xF0\x90\x8D\x88"; + cb.InsertString(0, hwair.data(), hwair.length(), startSequence); REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0); REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 3); REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf32) == 0); @@ -253,8 +256,8 @@ TEST_CASE("CharacterIndex") { cb.AllocateLineCharacterIndex(LineCharacterIndexType::Utf16 | LineCharacterIndexType::Utf32); bool startSequence = false; - const char *hwair = "a\xF0\x90\x8D\x88z"; - cb.InsertString(0, hwair, strlen(hwair), startSequence); + constexpr std::string_view hwair = "a\xF0\x90\x8D\x88z"; + cb.InsertString(0, hwair.data(), hwair.length(), startSequence); REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0); REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 4); @@ -283,8 +286,8 @@ TEST_CASE("CharacterIndex") { bool startSequence = false; // 3 lines of text containing 8 bytes - const char *data = "a\n\xF0\x90\x8D\x88\nz"; - cb.InsertString(0, data, strlen(data), startSequence); + constexpr std::string_view data = "a\n\xF0\x90\x8D\x88\nz"; + cb.InsertString(0, data.data(), data.length(), startSequence); REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0); REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 2); @@ -298,7 +301,7 @@ TEST_CASE("CharacterIndex") { // Insert a new line at end -> "a\n\xF0\x90\x8D\x88\nz\n" 4 lines // Last line empty - cb.InsertString(strlen(data), "\n", 1, startSequence); + cb.InsertString(data.length(), "\n", 1, startSequence); REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0); REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 2); @@ -313,7 +316,7 @@ TEST_CASE("CharacterIndex") { REQUIRE(cb.IndexLineStart(4, LineCharacterIndexType::Utf32) == 6); // Insert a new line before end -> "a\n\xF0\x90\x8D\x88\nz\n\n" 5 lines - cb.InsertString(strlen(data), "\n", 1, startSequence); + cb.InsertString(data.length(), "\n", 1, startSequence); REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0); REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 2); @@ -332,8 +335,8 @@ TEST_CASE("CharacterIndex") { // Insert a valid 3-byte UTF-8 character at start -> // "\xE2\x82\xACa\n\xF0\x90\x8D\x88\nz\n\n" 5 lines - const char *euro = "\xE2\x82\xAC"; - cb.InsertString(0, euro, strlen(euro), startSequence); + constexpr std::string_view euro = "\xE2\x82\xAC"; + cb.InsertString(0, euro.data(), euro.length(), startSequence); REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0); REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 3); @@ -353,8 +356,8 @@ TEST_CASE("CharacterIndex") { // "\xE2\x82\xACa\n\EF\xF0\x90\x8D\x88\nz\n\n" 5 lines // Should be treated as a single byte character - const char *lead = "\xEF"; - cb.InsertString(5, lead, strlen(lead), startSequence); + constexpr std::string_view lead = "\xEF"; + cb.InsertString(5, lead.data(), lead.length(), startSequence); REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0); REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 3); @@ -372,8 +375,8 @@ TEST_CASE("CharacterIndex") { // byte before and the 2 bytes after also be each treated as singles // so 3 more characters on line 0. - const char *ascii = "!"; - cb.InsertString(1, ascii, strlen(ascii), startSequence); + constexpr std::string_view ascii = "!"; + cb.InsertString(1, ascii.data(), ascii.length(), startSequence); REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0); REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 6); @@ -386,8 +389,8 @@ TEST_CASE("CharacterIndex") { // Insert a NEL after the '!' to trigger the utf8 line end case -> // "\xE2!\xC2\x85 \x82\xACa\n \EF\xF0\x90\x8D\x88\n z\n\n" 5 lines - const char *nel = "\xC2\x85"; - cb.InsertString(2, nel, strlen(nel), startSequence); + constexpr std::string_view nel = "\xC2\x85"; + cb.InsertString(2, nel.data(), nel.length(), startSequence); REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0); REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 3); @@ -406,11 +409,11 @@ TEST_CASE("CharacterIndex") { bool startSequence = false; // 3 lines of text containing 8 bytes - const char *data = "a\n\xF0\x90\x8D\x88\nz\nc"; - cb.InsertString(0, data, strlen(data), startSequence); + constexpr std::string_view data = "a\n\xF0\x90\x8D\x88\nz\nc"; + cb.InsertString(0, data.data(), data.length(), startSequence); // Delete first 2 new lines -> "az\nc" - cb.DeleteChars(1, strlen(data) - 4, startSequence); + cb.DeleteChars(1, data.length() - 4, startSequence); REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0); REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 3); @@ -427,8 +430,8 @@ TEST_CASE("CharacterIndex") { bool startSequence = false; // 3 lines of text containing 8 bytes - const char *data = "a\n\xF0\x90\x8D\x88\nz"; - cb.InsertString(0, data, strlen(data), startSequence); + constexpr std::string_view data = "a\n\xF0\x90\x8D\x88\nz"; + cb.InsertString(0, data.data(), data.length(), startSequence); // Delete lead byte from character on line 1 -> // "a\n\x90\x8D\x88\nz" @@ -461,8 +464,8 @@ TEST_CASE("CharacterIndex") { // Restore lead byte from character on line 0 making a 4-byte character -> // "a\xF0\x90\x8D\x88\nz" - const char *lead4 = "\xF0"; - cb.InsertString(1, lead4, strlen(lead4), startSequence); + constexpr std::string_view lead4 = "\xF0"; + cb.InsertString(1, lead4.data(), lead4.length(), startSequence); REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0); REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 4); @@ -479,13 +482,13 @@ TEST_CASE("CharacterIndex") { bool startSequence = false; // 2 lines of text containing 4 bytes - const char *data = "a\r\nb"; - cb.InsertString(0, data, strlen(data), startSequence); + constexpr std::string_view data = "a\r\nb"; + cb.InsertString(0, data.data(), data.length(), startSequence); // 3 lines of text containing 5 bytes -> // "a\r!\nb" - const char *ascii = "!"; - cb.InsertString(2, ascii, strlen(ascii), startSequence); + constexpr std::string_view ascii = "!"; + cb.InsertString(2, ascii.data(), ascii.length(), startSequence); REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0); REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 2); @@ -862,9 +865,9 @@ TEST_CASE("CellBufferWithChangeHistory") { SECTION("StraightUndoRedoSaveRevertRedo") { CellBuffer cb(true, false); cb.SetUndoCollection(false); - std::string sInsert = "abcdefghijklmnopqrstuvwxyz"; + constexpr std::string_view sInsert = "abcdefghijklmnopqrstuvwxyz"; bool startSequence = false; - cb.InsertString(0, sInsert.c_str(), sInsert.length(), startSequence); + cb.InsertString(0, sInsert.data(), sInsert.length(), startSequence); cb.SetUndoCollection(true); cb.SetSavePoint(); cb.ChangeHistorySet(true); @@ -1001,9 +1004,9 @@ TEST_CASE("CellBufferWithChangeHistory") { SECTION("Detached") { CellBuffer cb(true, false); cb.SetUndoCollection(false); - std::string sInsert = "abcdefghijklmnopqrstuvwxyz"; + constexpr std::string_view sInsert = "abcdefghijklmnopqrstuvwxyz"; bool startSequence = false; - cb.InsertString(0, sInsert.c_str(), sInsert.length(), startSequence); + cb.InsertString(0, sInsert.data(), sInsert.length(), startSequence); cb.SetUndoCollection(true); cb.SetSavePoint(); cb.ChangeHistorySet(true); diff --git a/test/unit/testDocument.cxx b/test/unit/testDocument.cxx index 863bf1959..85ed3b813 100644 --- a/test/unit/testDocument.cxx +++ b/test/unit/testDocument.cxx @@ -97,7 +97,7 @@ struct DocPlus { DocPlus(std::string_view svInitial, int codePage) : document(DocumentOption::Default) { SetCodePage(codePage); - document.InsertString(0, svInitial.data(), svInitial.length()); + document.InsertString(0, svInitial); } void SetCodePage(int codePage) { @@ -122,13 +122,13 @@ struct DocPlus { document.SetCaseFolder(std::move(pcft)); } - Sci::Position FindNeedle(const std::string &needle, FindOption options, Sci::Position *length) { + Sci::Position FindNeedle(std::string_view needle, FindOption options, Sci::Position *length) { assert(*length == static_cast<Sci::Position>(needle.length())); - return document.FindText(0, document.Length(), needle.c_str(), options, length); + return document.FindText(0, document.Length(), needle.data(), options, length); } - Sci::Position FindNeedleReverse(const std::string &needle, FindOption options, Sci::Position *length) { + Sci::Position FindNeedleReverse(std::string_view needle, FindOption options, Sci::Position *length) { assert(*length == static_cast<Sci::Position>(needle.length())); - return document.FindText(document.Length(), 0, needle.c_str(), options, length); + return document.FindText(document.Length(), 0, needle.data(), options, length); } void MoveGap(Sci::Position gapNew) { // Move gap to gapNew by inserting @@ -144,12 +144,12 @@ void TimeTrace(std::string_view sv, const Catch::Timer &tikka) { TEST_CASE("Document") { - const char sText[] = "Scintilla"; - const Sci::Position sLength = static_cast<Sci::Position>(strlen(sText)); + constexpr std::string_view sText = "Scintilla"; + constexpr Sci::Position sLength = sText.length(); SECTION("InsertOneLine") { DocPlus doc("", 0); - const Sci::Position length = doc.document.InsertString(0, sText, sLength); + const Sci::Position length = doc.document.InsertString(0, sText); REQUIRE(sLength == doc.document.Length()); REQUIRE(length == sLength); REQUIRE(1 == doc.document.LinesTotal()); @@ -167,42 +167,42 @@ TEST_CASE("Document") { // part way through a character. SECTION("SearchInLatin") { DocPlus doc("abcde", 0); // a b c d e - std::string finding = "b"; + constexpr std::string_view finding = "b"; Sci::Position lengthFinding = finding.length(); Sci::Position location = doc.FindNeedle(finding, FindOption::MatchCase, &lengthFinding); REQUIRE(location == 1); location = doc.FindNeedleReverse(finding, FindOption::MatchCase, &lengthFinding); REQUIRE(location == 1); - location = doc.document.FindText(0, 2, finding.c_str(), FindOption::MatchCase, &lengthFinding); + location = doc.document.FindText(0, 2, finding.data(), FindOption::MatchCase, &lengthFinding); REQUIRE(location == 1); - location = doc.document.FindText(0, 1, finding.c_str(), FindOption::MatchCase, &lengthFinding); + location = doc.document.FindText(0, 1, finding.data(), FindOption::MatchCase, &lengthFinding); REQUIRE(location == -1); } SECTION("SearchInBothSegments") { DocPlus doc("ab-ab", 0); // a b - a b - std::string finding = "ab"; + constexpr std::string_view finding = "ab"; for (int gapPos = 0; gapPos <= 5; gapPos++) { doc.MoveGap(gapPos); Sci::Position lengthFinding = finding.length(); - Sci::Position location = doc.document.FindText(0, doc.document.Length(), finding.c_str(), FindOption::MatchCase, &lengthFinding); + Sci::Position location = doc.document.FindText(0, doc.document.Length(), finding.data(), FindOption::MatchCase, &lengthFinding); REQUIRE(location == 0); - location = doc.document.FindText(2, doc.document.Length(), finding.c_str(), FindOption::MatchCase, &lengthFinding); + location = doc.document.FindText(2, doc.document.Length(), finding.data(), FindOption::MatchCase, &lengthFinding); REQUIRE(location == 3); } } SECTION("InsensitiveSearchInLatin") { DocPlus doc("abcde", 0); // a b c d e - std::string finding = "B"; + constexpr std::string_view finding = "B"; Sci::Position lengthFinding = finding.length(); Sci::Position location = doc.FindNeedle(finding, FindOption::None, &lengthFinding); REQUIRE(location == 1); location = doc.FindNeedleReverse(finding, FindOption::None, &lengthFinding); REQUIRE(location == 1); - location = doc.document.FindText(0, 2, finding.c_str(), FindOption::None, &lengthFinding); + location = doc.document.FindText(0, 2, finding.data(), FindOption::None, &lengthFinding); REQUIRE(location == 1); - location = doc.document.FindText(0, 1, finding.c_str(), FindOption::None, &lengthFinding); + location = doc.document.FindText(0, 1, finding.data(), FindOption::None, &lengthFinding); REQUIRE(location == -1); } @@ -212,11 +212,11 @@ TEST_CASE("Document") { doc.SetSBCSFoldings(foldings1252, std::size(foldings1252)); // Search for upper-case AE - std::string finding = "\xc6"; + std::string_view finding = "\xc6"; Sci::Position lengthFinding = finding.length(); Sci::Position location = doc.FindNeedle(finding, FindOption::None, &lengthFinding); REQUIRE(location == 3); - location = doc.document.FindText(4, doc.document.Length(), finding.c_str(), FindOption::None, &lengthFinding); + location = doc.document.FindText(4, doc.document.Length(), finding.data(), FindOption::None, &lengthFinding); REQUIRE(location == 5); location = doc.FindNeedleReverse(finding, FindOption::None, &lengthFinding); REQUIRE(location == 5); @@ -225,7 +225,7 @@ TEST_CASE("Document") { finding = "\xe6"; location = doc.FindNeedle(finding, FindOption::None, &lengthFinding); REQUIRE(location == 3); - location = doc.document.FindText(4, doc.document.Length(), finding.c_str(), FindOption::None, &lengthFinding); + location = doc.document.FindText(4, doc.document.Length(), finding.data(), FindOption::None, &lengthFinding); REQUIRE(location == 5); location = doc.FindNeedleReverse(finding, FindOption::None, &lengthFinding); REQUIRE(location == 5); @@ -234,12 +234,12 @@ TEST_CASE("Document") { SECTION("Search2InLatin") { // Checks that the initial '_' and final 'f' are ignored since they are outside the search bounds DocPlus doc("_abcdef", 0); // _ a b c d e f - std::string finding = "cd"; + constexpr std::string_view finding = "cd"; Sci::Position lengthFinding = finding.length(); size_t docLength = doc.document.Length() - 1; - Sci::Position location = doc.document.FindText(1, docLength, finding.c_str(), FindOption::MatchCase, &lengthFinding); + Sci::Position location = doc.document.FindText(1, docLength, finding.data(), FindOption::MatchCase, &lengthFinding); REQUIRE(location == 3); - location = doc.document.FindText(docLength, 1, finding.c_str(), FindOption::MatchCase, &lengthFinding); + location = doc.document.FindText(docLength, 1, finding.data(), FindOption::MatchCase, &lengthFinding); REQUIRE(location == 3); location = doc.document.FindText(docLength, 1, "bc", FindOption::MatchCase, &lengthFinding); REQUIRE(location == 2); @@ -258,46 +258,46 @@ TEST_CASE("Document") { SECTION("SearchInUTF8") { DocPlus doc("ab\xCE\x93" "d", CpUtf8); // a b gamma d - const std::string finding = "b"; + constexpr std::string_view finding = "b"; Sci::Position lengthFinding = finding.length(); Sci::Position location = doc.FindNeedle(finding, FindOption::MatchCase, &lengthFinding); REQUIRE(location == 1); - location = doc.document.FindText(doc.document.Length(), 0, finding.c_str(), FindOption::MatchCase, &lengthFinding); + location = doc.document.FindText(doc.document.Length(), 0, finding.data(), FindOption::MatchCase, &lengthFinding); REQUIRE(location == 1); - location = doc.document.FindText(0, 1, finding.c_str(), FindOption::MatchCase, &lengthFinding); + location = doc.document.FindText(0, 1, finding.data(), FindOption::MatchCase, &lengthFinding); REQUIRE(location == -1); // Check doesn't try to follow a lead-byte past the search end - const std::string findingUTF = "\xCE\x93"; + constexpr std::string_view findingUTF = "\xCE\x93"; lengthFinding = findingUTF.length(); - location = doc.document.FindText(0, 4, findingUTF.c_str(), FindOption::MatchCase, &lengthFinding); + location = doc.document.FindText(0, 4, findingUTF.data(), FindOption::MatchCase, &lengthFinding); REQUIRE(location == 2); // Only succeeds as 3 is partway through character so adjusted to 4 - location = doc.document.FindText(0, 3, findingUTF.c_str(), FindOption::MatchCase, &lengthFinding); + location = doc.document.FindText(0, 3, findingUTF.data(), FindOption::MatchCase, &lengthFinding); REQUIRE(location == 2); - location = doc.document.FindText(0, 2, findingUTF.c_str(), FindOption::MatchCase, &lengthFinding); + location = doc.document.FindText(0, 2, findingUTF.data(), FindOption::MatchCase, &lengthFinding); REQUIRE(location == -1); } SECTION("InsensitiveSearchInUTF8") { DocPlus doc("ab\xCE\x93" "d", CpUtf8); // a b gamma d - const std::string finding = "b"; + constexpr std::string_view finding = "b"; Sci::Position lengthFinding = finding.length(); Sci::Position location = doc.FindNeedle(finding, FindOption::None, &lengthFinding); REQUIRE(location == 1); - location = doc.document.FindText(doc.document.Length(), 0, finding.c_str(), FindOption::None, &lengthFinding); + location = doc.document.FindText(doc.document.Length(), 0, finding.data(), FindOption::None, &lengthFinding); REQUIRE(location == 1); - const std::string findingUTF = "\xCE\x93"; + constexpr std::string_view findingUTF = "\xCE\x93"; lengthFinding = findingUTF.length(); location = doc.FindNeedle(findingUTF, FindOption::None, &lengthFinding); REQUIRE(location == 2); - location = doc.document.FindText(doc.document.Length(), 0, findingUTF.c_str(), FindOption::None, &lengthFinding); + location = doc.document.FindText(doc.document.Length(), 0, findingUTF.data(), FindOption::None, &lengthFinding); REQUIRE(location == 2); - location = doc.document.FindText(0, 4, findingUTF.c_str(), FindOption::None, &lengthFinding); + location = doc.document.FindText(0, 4, findingUTF.data(), FindOption::None, &lengthFinding); REQUIRE(location == 2); // Only succeeds as 3 is partway through character so adjusted to 4 - location = doc.document.FindText(0, 3, findingUTF.c_str(), FindOption::None, &lengthFinding); + location = doc.document.FindText(0, 3, findingUTF.data(), FindOption::None, &lengthFinding); REQUIRE(location == 2); - location = doc.document.FindText(0, 2, findingUTF.c_str(), FindOption::None, &lengthFinding); + location = doc.document.FindText(0, 2, findingUTF.data(), FindOption::None, &lengthFinding); REQUIRE(location == -1); } @@ -306,14 +306,14 @@ TEST_CASE("Document") { // The 'b' can be incorrectly matched by the search string 'b' when the search // does not iterate the text correctly. DocPlus doc("ab\xe9" "b ", 932); // a b {CJK UNIFIED IDEOGRAPH-9955} {space} - std::string finding = "b"; + constexpr std::string_view finding = "b"; // Search forwards Sci::Position lengthFinding = finding.length(); Sci::Position location = doc.FindNeedle(finding, FindOption::MatchCase, &lengthFinding); REQUIRE(location == 1); // Search backwards lengthFinding = finding.length(); - location = doc.document.FindText(doc.document.Length(), 0, finding.c_str(), FindOption::MatchCase, &lengthFinding); + location = doc.document.FindText(doc.document.Length(), 0, finding.data(), FindOption::MatchCase, &lengthFinding); REQUIRE(location == 1); } @@ -322,27 +322,27 @@ TEST_CASE("Document") { // The 'b' can be incorrectly matched by the search string 'b' when the search // does not iterate the text correctly. DocPlus doc("ab\xe9" "b ", 932); // a b {CJK UNIFIED IDEOGRAPH-9955} {space} - std::string finding = "b"; + constexpr std::string_view finding = "b"; // Search forwards Sci::Position lengthFinding = finding.length(); Sci::Position location = doc.FindNeedle(finding, FindOption::None, &lengthFinding); REQUIRE(location == 1); // Search backwards lengthFinding = finding.length(); - location = doc.document.FindText(doc.document.Length(), 0, finding.c_str(), FindOption::None, &lengthFinding); + location = doc.document.FindText(doc.document.Length(), 0, finding.data(), FindOption::None, &lengthFinding); REQUIRE(location == 1); - std::string finding932 = "\xe9" "b"; + constexpr std::string_view finding932 = "\xe9" "b"; // Search forwards lengthFinding = finding932.length(); location = doc.FindNeedle(finding932, FindOption::None, &lengthFinding); REQUIRE(location == 2); // Search backwards lengthFinding = finding932.length(); - location = doc.document.FindText(doc.document.Length(), 0, finding932.c_str(), FindOption::None, &lengthFinding); + location = doc.document.FindText(doc.document.Length(), 0, finding932.data(), FindOption::None, &lengthFinding); REQUIRE(location == 2); - location = doc.document.FindText(0, 3, finding932.c_str(), FindOption::None, &lengthFinding); + location = doc.document.FindText(0, 3, finding932.data(), FindOption::None, &lengthFinding); REQUIRE(location == 2); - location = doc.document.FindText(0, 2, finding932.c_str(), FindOption::None, &lengthFinding); + location = doc.document.FindText(0, 2, finding932.data(), FindOption::None, &lengthFinding); REQUIRE(location == -1); // Can not test case mapping of double byte text as folder available here does not implement this } @@ -427,8 +427,8 @@ TEST_CASE("Document") { // O p e n = U+958B Ku ( O ) U+7DE8 - // U+958B open // U+7DE8 arrange - const std::string japaneseText = "Open=\x8aJ\x82\xad(O)\x95\xd2-"; - const Sci::Position length = doc.InsertString(0, japaneseText.c_str(), japaneseText.length()); + constexpr std::string_view japaneseText = "Open=\x8aJ\x82\xad(O)\x95\xd2-"; + const Sci::Position length = doc.InsertString(0, japaneseText); REQUIRE(length == 15); // Forwards REQUIRE(doc.NextPosition( 0, 1) == 1); @@ -468,13 +468,13 @@ TEST_CASE("Document") { SECTION("RegexSearchAndSubstitution") { DocPlus doc("\n\r\r\n 1a\xCE\x93z \n\r\r\n 2b\xCE\x93y \n\r\r\n", CpUtf8);// 1a gamma z 2b gamma y - const std::string finding = R"(\d+(\w+))"; + constexpr std::string_view finding = R"(\d+(\w+))"; Sci::Position lengthFinding = finding.length(); Sci::Position location = doc.FindNeedle(finding, FindOption::RegExp | FindOption::Posix, &lengthFinding); REQUIRE(location == 5); REQUIRE(lengthFinding == 5); - const std::string_view substituteText = R"(\t\1\n)"; + constexpr std::string_view substituteText = R"(\t\1\n)"; Sci::Position lengthsubstitute = substituteText.length(); std::string substituted = doc.document.SubstituteByPosition(substituteText.data(), &lengthsubstitute); REQUIRE(lengthsubstitute == 6); @@ -549,7 +549,7 @@ TEST_CASE("SafeSegment") { SECTION("Short") { const DocPlus doc("", 0); // all encoding: break before or after last space - const std::string_view text = "12 "; + constexpr std::string_view text = "12 "; size_t length = doc.document.SafeSegment(text); REQUIRE(length <= text.length()); REQUIRE(text[length - 1] == '2'); diff --git a/test/unit/testUniConversion.cxx b/test/unit/testUniConversion.cxx index 5e0225568..9c313390a 100644 --- a/test/unit/testUniConversion.cxx +++ b/test/unit/testUniConversion.cxx @@ -255,8 +255,8 @@ TEST_CASE("UniConversion") { namespace { // Simple adapter to avoid casting -int UTFClass(const char *s) noexcept { - return UTF8Classify(reinterpret_cast<const unsigned char *>(s), static_cast<int>(strlen(s))); +int UTFClass(std::string_view sv) noexcept { + return UTF8Classify(sv); } } |