Skip to content

Commit

Permalink
Add support for "XXX"^^foo:typename Value Literals
Browse files Browse the repository at this point in the history
This requires searching for the ':' from the right because it may appear
in dateTime strings. Do the same for the '#' to keep it symmetric.
  • Loading branch information
niklas88 committed Dec 21, 2017
1 parent a9184f3 commit d99c50d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/util/Conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,38 @@ inline bool isXsdValue(const string val);

// _____________________________________________________________________________
string convertValueLiteralToIndexWord(const string& orig) {
/*
* Value literals can have one of two forms
* 0) "123"^^<http://www.w3.org/2001/XMLSchema#integer>
* 1) "123"^^xsd:integer
*
* TODO: This ignores the URI such that xsd:integer == foo:integer ==
* <http://baz#integer>
*/
assert(orig.size() > 0);
assert(orig[0] == '\"');
assert(orig[orig.size() - 1] == '>');
size_t posOfSecondQuote = orig.find('\"', 1);
size_t posOfHashTag = orig.find('#');
string value;
string type;
size_t posOfSecondQuote = orig.rfind('\"');
assert(posOfSecondQuote != string::npos);
assert(posOfHashTag != string::npos);
// -1 for the quote and since substr takes a length
// not an end position
value = orig.substr(1, posOfSecondQuote - 1);
if (orig[orig.size() - 1] == '>') {
size_t posOfHashTag = orig.rfind('#');
assert(posOfHashTag != string::npos);

// +2 for '>' and Hashtag
type = orig.substr(posOfHashTag + 1,
orig.size() - (posOfHashTag + 2));
} else {
size_t posOfDoubleDot = orig.rfind(':');
assert(posOfDoubleDot != string::npos);

string value = orig.substr(1, posOfSecondQuote - 1);
string type = orig.substr(posOfHashTag + 1,
orig.size() - (posOfHashTag + 2));
// +1 for double dot
type = orig.substr(posOfDoubleDot + 1,
orig.size() - (posOfDoubleDot + 1));
}

if (type == "dateTime" || type == "gYear" || type == "gYearMonth"
|| type == "date") {
Expand Down
23 changes: 23 additions & 0 deletions test/ConversionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,35 @@ TEST(ConversionsTest, convertDateToIndexWord) {
TEST(ConversionsTest, endToEndDate) {
string in = "\"1990-01-01T00:00:00\"^^<http://www.w3.org/2001/XMLSchema#dateTime>";
string in2 = "\"1990-01-01\"^^<http://www.w3.org/2001/XMLSchema#date>";
string in3 = "\"1990-01-01\"^^xsd:date";
string in4 = "\"1990-01-01T00:00:00\"^^xsd:dateTime";
string indexW = ":v:date:0000000000000001990-01-01T00:00:00";
ASSERT_EQ(indexW, convertValueLiteralToIndexWord(in));
ASSERT_EQ(indexW, convertValueLiteralToIndexWord(in2));
ASSERT_EQ(indexW, convertValueLiteralToIndexWord(in3));
ASSERT_EQ(indexW, convertValueLiteralToIndexWord(in4));
ASSERT_EQ(in, convertIndexWordToValueLiteral(indexW));
}

TEST(ConversionsTest, shortFormEquivalence) {
string in = "\"1230.7\"^^<http://www.w3.org/2001/XMLSchema#float>";
string in_short = "\"1230.7\"^^xsd:float";
string nin = "\"-1230.7\"^^<http://www.w3.org/2001/XMLSchema#float>";
string nin_short = "\"-1230.7\"^^xsd:float";
string in2 = "\"1000\"^^<http://www.w3.org/2001/XMLSchema#int>";
string in2_short = "\"1000\"^^xsd:int";
string nin2 = "\"-1000\"^^<http://www.w3.org/2001/XMLSchema#int>";
string nin2_short = "\"-1000\"^^xsd:int";
ASSERT_EQ(convertValueLiteralToIndexWord(in),
convertValueLiteralToIndexWord(in_short));
ASSERT_EQ(convertValueLiteralToIndexWord(nin),
convertValueLiteralToIndexWord(nin_short));
ASSERT_EQ(convertValueLiteralToIndexWord(in2),
convertValueLiteralToIndexWord(in2_short));
ASSERT_EQ(convertValueLiteralToIndexWord(nin2),
convertValueLiteralToIndexWord(nin2_short));
}

TEST(ConversionsTest, endToEndNumbers) {
string in = "\"1000\"^^<http://www.w3.org/2001/XMLSchema#int>";
string nin = "\"-1000\"^^<http://www.w3.org/2001/XMLSchema#int>";
Expand Down

0 comments on commit d99c50d

Please sign in to comment.