TEST(OwnedStringTest, copy_constructor_original_copy) { // Initialize a mutable string. const char *original = "string"; const size_t length = strlen(original); char *data = static_cast<char *>(malloc(length + 1)); memcpy(data, original, length); data[length] = '\0'; // Create an OwnedString. OwnedString ownedString(data, length); EXPECT_EQ(length, ownedString.size()); EXPECT_FALSE(ownedString.empty()); // Copy the string OwnedString copy = OwnedString(ownedString.copy()); EXPECT_EQ(length, copy.size()); EXPECT_FALSE(copy.empty()); // Make sure we correctly copied the data and that it is null // terminated. StringRef str = copy.str(); EXPECT_EQ("string", str); EXPECT_EQ(length, strlen(str.data())); // Make sure updating the original pointer doesn't affect the copy. data[0] = 'a'; EXPECT_EQ("string", str); }
TEST(OwnedStringTest, char_pointer_non_empty) { const char *data = "string"; const size_t length = strlen(data); OwnedString ownedString = OwnedString::makeUnowned(data); EXPECT_EQ(length, ownedString.size()); EXPECT_FALSE(ownedString.empty()); EXPECT_EQ(data, ownedString.str().data()); }
TEST(OwnedStringTest, char_pointer_length_zero) { const char *data = "string"; const size_t length = 0; OwnedString ownedString(data, length); EXPECT_EQ(length, ownedString.size()); EXPECT_TRUE(ownedString.empty()); OwnedString copy = ownedString.copy(); EXPECT_EQ(length, copy.size()); EXPECT_TRUE(copy.empty()); }
TEST(OwnedStringTest, char_pointer_non_empty) { const char *data = "string"; const size_t length = strlen(data); OwnedString ownedString(data); EXPECT_EQ(length, ownedString.size()); EXPECT_FALSE(ownedString.empty()); OwnedString copy = ownedString.copy(); EXPECT_EQ(length, copy.size()); EXPECT_FALSE(copy.empty()); StringRef str = copy.str(); EXPECT_EQ("string", str); EXPECT_EQ(length, strlen(str.data())); }
TEST(OwnedStringTest, char_pointer_length_nonzero) { const char *data = "string"; const size_t length = 1; OwnedString ownedString(data, length); EXPECT_EQ(length, ownedString.size()); EXPECT_FALSE(ownedString.empty()); OwnedString copy = ownedString.copy(); EXPECT_EQ(length, copy.size()); EXPECT_FALSE(copy.empty()); // Make sure we correctly copied the data and that it is null // terminated. StringRef str = copy.str(); EXPECT_EQ("s", str); EXPECT_EQ(1UL, strlen(str.data())); }
RC<TokenSyntax> syntax::findTokenSyntax(tok ExpectedKind, OwnedString ExpectedText, SourceManager &SourceMgr, SourceLoc Loc, unsigned BufferID, const TokenPositionList &Tokens) { auto Offset = SourceMgr.getLocOffsetInBuffer(Loc, BufferID); size_t Start = 0; size_t End = Tokens.size() - 1; while (Start <= End) { auto Mid = (Start + End) / 2; auto TokAndPos = Tokens[Mid]; auto Tok = TokAndPos.first; auto Pos = TokAndPos.second; auto TokStart = Pos.getOffset(); auto TokEnd = TokStart + Tok->getText().size(); if (Offset == TokStart) { if (Tok->getTokenKind() == ExpectedKind && (ExpectedText.empty() || Tok->getText() == ExpectedText.str())) { return Tok; } else { return TokenSyntax::missingToken(ExpectedKind, ExpectedText); } } if (TokStart < Offset) { Start = Mid + 1; } else { End = Mid - 1; } } return TokenSyntax::missingToken(ExpectedKind, ExpectedText); }