Skip to content

Commit

Permalink
Bugfix #284 revert connstring string regex
Browse files Browse the repository at this point in the history
  • Loading branch information
mbdavid committed Aug 24, 2016
1 parent 5d22062 commit 659c41a
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 64 deletions.
2 changes: 1 addition & 1 deletion LiteDB.Core.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>LiteDB.Core</id>
<version>2.0.1</version>
<version>2.0.2</version>
<title>LiteDB Portable</title>
<authors>Mauricio David</authors>
<projectUrl>http://www.litedb.org</projectUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: NeutralResourcesLanguage("en")]
[assembly: AssemblyVersion("2.0.1.0")]
[assembly: AssemblyFileVersion("2.0.1.0")]
[assembly: AssemblyVersion("2.0.2.0")]
[assembly: AssemblyFileVersion("2.0.2.0")]
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("dbe3535c-60f3-4e17-9a05-abe280d35b74")]
[assembly: AssemblyVersion("2.0.1.0")]
[assembly: AssemblyFileVersion("2.0.1.0")]
[assembly: AssemblyVersion("2.0.2.0")]
[assembly: AssemblyFileVersion("2.0.2.0")]
4 changes: 2 additions & 2 deletions LiteDB.Shell/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("01ce385b-31a7-4b1a-9487-23fe8acb3888")]
[assembly: AssemblyVersion("2.0.1.0")]
[assembly: AssemblyFileVersion("2.0.1.0")]
[assembly: AssemblyVersion("2.0.2.0")]
[assembly: AssemblyFileVersion("2.0.2.0")]
[assembly: NeutralResourcesLanguage("en")]

4 changes: 2 additions & 2 deletions LiteDB.Tests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
[assembly: NeutralResourcesLanguage("en")]
[assembly: ComVisible(false)]
[assembly: Guid("de183e83-7df6-475c-8185-b0070d098821")]
[assembly: AssemblyVersion("2.0.1.0")]
[assembly: AssemblyFileVersion("2.0.1.0")]
[assembly: AssemblyVersion("2.0.2.0")]
[assembly: AssemblyFileVersion("2.0.2.0")]
2 changes: 1 addition & 1 deletion LiteDB.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>LiteDB</id>
<version>2.0.1</version>
<version>2.0.2</version>
<title>LiteDB</title>
<authors>Mauricio David</authors>
<projectUrl>http://www.litedb.org</projectUrl>
Expand Down
4 changes: 2 additions & 2 deletions LiteDB/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("54989b5c-4bcf-4d58-b8ba-9b014a324f76")]
[assembly: AssemblyVersion("2.0.1.0")]
[assembly: AssemblyFileVersion("2.0.1.0")]
[assembly: AssemblyVersion("2.0.2.0")]
[assembly: AssemblyFileVersion("2.0.2.0")]
[assembly: NeutralResourcesLanguage("en")]
66 changes: 14 additions & 52 deletions LiteDB/Utils/ConnectionString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,65 +13,27 @@ public class ConnectionString
{
private Dictionary<string, string> _values;

private static readonly Regex keyValuePairRegex = new Regex(
@"^(
;*
(?<pair>
((?<key>[a-zA-Z][a-zA-Z0-9-_]*)=)?
(?<value>
(?<quotedValue>
(?<quote>['""])
((\\\k<quote>)|((?!\k<quote>).))*
\k<quote>?
)
|(?<simpleValue>[^\s]+)
)
)
;*
)*$",
RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace |
RegexOptions.ExplicitCapture
);

public ConnectionString(string connectionString)
{
if (string.IsNullOrEmpty(connectionString)) throw new ArgumentNullException("connectionString");

_values = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Match match = keyValuePairRegex.Match(connectionString);
foreach (Capture pair in match.Groups["pair"].Captures)
// Create a dictionary from string name=value collection
if (connectionString.Contains("="))
{
var key =
match.Groups["key"].Captures.Cast<Capture>()
.FirstOrDefault(
keyMatch =>
keyMatch.Index >= pair.Index && keyMatch.Index <= pair.Index + pair.Length);
var value =
match.Groups["value"].Captures.Cast<Capture>()
.FirstOrDefault(
valueMatch =>
valueMatch.Index >= pair.Index && valueMatch.Index <= pair.Index + pair.Length);
if (key == null && value != null)
{
_values.Add(value.Value.Trim().Trim('"'), null);
}
if (key != null)
{
_values.Add(key.Value.Trim().Trim('"'), value?.Value.Trim().Trim('"'));
}
_values = connectionString.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
.Select(t => t.Split(new char[] { '=' }, 2))
.ToDictionary(t => t[0].Trim().ToLower(), t => t.Length == 1 ? "" : t[1].Trim(), StringComparer.OrdinalIgnoreCase);
}

// return if there is more than one pair
// or the only pair's name is 'filename'
// or the only pair's value is not null
if (_values.Count > 1 || _values.ContainsKey("filename") || !_values.ContainsValue(null))
return;

_values["filename"] = (_values.Count > 0 ? _values.Keys.FirstOrDefault() : connectionString.Trim().Trim('"')) ??
string.Empty;
#if !PCL
_values["filename"] = Path.GetFullPath(_values["filename"]);
else
{
// If connectionstring is only a filename, set filename
_values = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
#if PCL
_values["filename"] = connectionString;
#else
_values["filename"] = Path.GetFullPath(connectionString);
#endif
}
}

public T GetValue<T>(string key, T defaultValue)
Expand Down

0 comments on commit 659c41a

Please sign in to comment.