Skip to content

Commit

Permalink
Slight code quality improvements and optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco De Salvo committed Mar 14, 2024
1 parent 52d929c commit 6b56c3d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 28 deletions.
12 changes: 5 additions & 7 deletions RDFSharp/Model/Serializers/RDFNTriples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ internal static void Serialize(RDFGraph graph, Stream outputStream)
.Replace("\r", "\\r");

#region plain literal
if (t.Object is RDFPlainLiteral)
tripleTemplate = ((RDFPlainLiteral)t.Object).HasLanguage() ? tripleTemplate.Replace("{LANG}", ((RDFPlainLiteral)t.Object).Language)
: tripleTemplate.Replace("@{LANG}", string.Empty);
if (t.Object is RDFPlainLiteral plitObj)
tripleTemplate = plitObj.HasLanguage() ? tripleTemplate.Replace("{LANG}", plitObj.Language)
: tripleTemplate.Replace("@{LANG}", string.Empty);
#endregion

#region typed literal
Expand Down Expand Up @@ -273,8 +273,8 @@ internal static RDFGraph Deserialize(Stream inputStream, Uri graphContext)

#region plain literal
if (!tokens[2].Contains("^^")
|| tokens[2].EndsWith("^^")
|| tokens[2].Substring(tokens[2].LastIndexOf("^^", StringComparison.Ordinal) + 2, 1) != "<")
|| tokens[2].EndsWith("^^")
|| tokens[2].Substring(tokens[2].LastIndexOf("^^", StringComparison.Ordinal) + 2, 1) != "<")
{
if (regexLPL.Value.Match(tokens[2]).Success)
{
Expand All @@ -285,9 +285,7 @@ internal static RDFGraph Deserialize(Stream inputStream, Uri graphContext)
L = new RDFPlainLiteral(HttpUtility.HtmlDecode(pLitValue), pLitLang);
}
else
{
L = new RDFPlainLiteral(HttpUtility.HtmlDecode(tokens[2]));
}
}
#endregion

Expand Down
8 changes: 3 additions & 5 deletions RDFSharp/Query/RDFQueryUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ public static RDFPatternMember ParseRDFPatternMember(string pMember)
#region Plain Literal
int lastIndexOfDatatype = pMember.LastIndexOf("^^", StringComparison.OrdinalIgnoreCase);
if (!pMember.Contains("^^")
|| pMember.EndsWith("^^")
|| RDFModelUtilities.GetUriFromString(pMember.Substring(lastIndexOfDatatype + 2)) == null)
|| pMember.EndsWith("^^")
|| RDFModelUtilities.GetUriFromString(pMember.Substring(lastIndexOfDatatype + 2)) == null)
{
RDFPlainLiteral pLit = null;
RDFPlainLiteral pLit;
if (RDFNTriples.regexLPL.Value.Match(pMember).Success)
{
int lastIndexOfLanguage = pMember.LastIndexOf("@", StringComparison.OrdinalIgnoreCase);
Expand All @@ -59,9 +59,7 @@ public static RDFPatternMember ParseRDFPatternMember(string pMember)
pLit = new RDFPlainLiteral(pLitVal, pLitLng);
}
else
{
pLit = new RDFPlainLiteral(pMember);
}
return pLit;
}
#endregion
Expand Down
6 changes: 3 additions & 3 deletions RDFSharp/Store/RDFStoreUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ public static RDFQuadruple ParseQuadruple(IDataReader fetchedQuadruples)
//PlainLiteral
int lastIndexOfDatatype = literal.LastIndexOf("^^", StringComparison.OrdinalIgnoreCase);
if (!literal.Contains("^^")
|| literal.EndsWith("^^")
|| RDFModelUtilities.GetUriFromString(literal.Substring(lastIndexOfDatatype + 2)) == null)
|| literal.EndsWith("^^")
|| RDFModelUtilities.GetUriFromString(literal.Substring(lastIndexOfDatatype + 2)) == null)
{
RDFPlainLiteral pLit = null;
RDFPlainLiteral pLit;
if (RDFNTriples.regexLPL.Value.Match(literal).Success)
{
int lastIndexOfLanguage = literal.LastIndexOf("@", StringComparison.OrdinalIgnoreCase);
Expand Down
18 changes: 5 additions & 13 deletions RDFSharp/Store/Serializers/RDFNQuads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,14 @@ internal static void Serialize(RDFStore store, Stream outputStream)
.Replace("\r", "\\r");

#region plain literal
if (q.Object is RDFPlainLiteral)
{
if (((RDFPlainLiteral)q.Object).Language != string.Empty)
quadrupleTemplate = quadrupleTemplate.Replace("{LANG}", ((RDFPlainLiteral)q.Object).Language);
else
quadrupleTemplate = quadrupleTemplate.Replace("@{LANG}", string.Empty);
}
if (q.Object is RDFPlainLiteral plitObj)
quadrupleTemplate = plitObj.HasLanguage() ? quadrupleTemplate.Replace("{LANG}", plitObj.Language)
: quadrupleTemplate.Replace("@{LANG}", string.Empty);
#endregion

#region typed literal
else
{
quadrupleTemplate = quadrupleTemplate.Replace("{DTYPE}", RDFModelUtilities.GetDatatypeFromEnum(((RDFTypedLiteral)q.Object).Datatype));
}
#endregion
}
#endregion
Expand Down Expand Up @@ -275,8 +269,8 @@ internal static RDFMemoryStore Deserialize(Stream inputStream)

#region plain literal
if (!tokens[2].Contains("^^")
|| tokens[2].EndsWith("^^")
|| tokens[2].Substring(tokens[2].LastIndexOf("^^", StringComparison.Ordinal) + 2, 1) != "<")
|| tokens[2].EndsWith("^^")
|| tokens[2].Substring(tokens[2].LastIndexOf("^^", StringComparison.Ordinal) + 2, 1) != "<")
{
if (RDFNTriples.regexLPL.Value.Match(tokens[2]).Success)
{
Expand All @@ -287,9 +281,7 @@ internal static RDFMemoryStore Deserialize(Stream inputStream)
L = new RDFPlainLiteral(HttpUtility.HtmlDecode(pLitValue), pLitLang);
}
else
{
L = new RDFPlainLiteral(HttpUtility.HtmlDecode(tokens[2]));
}
}
#endregion

Expand Down

0 comments on commit 6b56c3d

Please sign in to comment.