-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2417bd
commit 54e67d7
Showing
6 changed files
with
89 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Source: https://github.com/JosefNemec/Playnite.git | ||
Commit: 2afc8359d8c45e80c66404add4221037ce600d43 | ||
Commit: fdb2a8652a34fc2ff08ec5a416f2cb50e85907e7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Source: https://github.com/JosefNemec/PlayniteExtensions.git | ||
Commit: 7c471edfeea4ed1384f97c14ad76c48988453248 | ||
Commit: a78ac2cc75d696110eb51c179b91ef9cf5090fa2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Source: https://github.com/hawkeye116477/playnite-common-plugin.git | ||
Commit: c8b11813f97cf97ae65b31a7289ea8001ef763b8 | ||
Commit: 1d84bcea3a040e82f24e6a4965a06fd1097a038a |
82 changes: 82 additions & 0 deletions
82
third_party/playnite-common-plugin/src/SplitOutsideQuotesExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace CommonPlugin | ||
{ | ||
// Source: https://gist.github.com/mrpeardotnet/cba4338ffe01cb6e41d2765d8886aded | ||
public static class SplitOutsideQuotesExtensions | ||
{ | ||
/// <summary> | ||
/// Splits the string by specified separator, but only when the separator is outside the quotes. | ||
/// </summary> | ||
/// <param name="source">The source string to separate.</param> | ||
/// <param name="splitChar">The character used to split strings.</param> | ||
/// <param name="trimSplits">If set to <c>true</c>, split strings are trimmed (whitespaces are removed).</param> | ||
/// <param name="ignoreEmptyResults">If set to <c>true</c>, empty split results are ignored (not included in the result).</param> | ||
/// <param name="preserveEscapeCharInQuotes">If set to <c>true</c>, then the escape character (\) used to escape e.g. quotes is included in the results.</param> | ||
public static string[] SplitOutsideQuotes(this string source, char separator, bool trimSplits = true, bool ignoreEmptyResults = true, bool preserveEscapeCharInQuotes = true) | ||
{ | ||
return source.SplitOutsideQuotes(new[] { separator }, trimSplits, ignoreEmptyResults, preserveEscapeCharInQuotes); | ||
} | ||
|
||
/// <summary> | ||
/// Splits the string by specified separator, but only when the separator is outside the quotes. | ||
/// </summary> | ||
/// <param name="source">The source string to separate.</param> | ||
/// <param name="splitChars">The characters used to split strings.</param> | ||
/// <param name="trimSplits">If set to <c>true</c>, split strings are trimmed (whitespaces are removed).</param> | ||
/// <param name="ignoreEmptyResults">If set to <c>true</c>, empty split results are ignored (not included in the result).</param> | ||
/// <param name="preserveEscapeCharInQuotes">If set to <c>true</c>, then the escape character (\) used to escape e.g. quotes is included in the results.</param> | ||
public static string[] SplitOutsideQuotes(this string source, char[] separators, bool trimSplits = true, bool ignoreEmptyResults = true, bool preserveEscapeCharInQuotes = true) | ||
{ | ||
if (source == null) return null; | ||
|
||
var result = new List<string>(); | ||
var escapeFlag = false; | ||
var quotesOpen = false; | ||
var currentItem = new StringBuilder(); | ||
|
||
foreach (var currentChar in source) | ||
{ | ||
if (escapeFlag) | ||
{ | ||
currentItem.Append(currentChar); | ||
escapeFlag = false; | ||
continue; | ||
} | ||
|
||
if (separators.Contains(currentChar) && !quotesOpen) | ||
{ | ||
var currentItemString = trimSplits ? currentItem.ToString().Trim() : currentItem.ToString(); | ||
currentItem.Clear(); | ||
if (string.IsNullOrEmpty(currentItemString) && ignoreEmptyResults) continue; | ||
result.Add(currentItemString); | ||
continue; | ||
} | ||
|
||
switch (currentChar) | ||
{ | ||
default: | ||
currentItem.Append(currentChar); | ||
break; | ||
case '\\': | ||
if (quotesOpen && preserveEscapeCharInQuotes) currentItem.Append(currentChar); | ||
escapeFlag = true; | ||
break; | ||
case '"': | ||
currentItem.Append(currentChar); | ||
quotesOpen = !quotesOpen; | ||
break; | ||
} | ||
} | ||
|
||
if (escapeFlag) currentItem.Append("\\"); | ||
|
||
var lastCurrentItemString = trimSplits ? currentItem.ToString().Trim() : currentItem.ToString(); | ||
if (!(string.IsNullOrEmpty(lastCurrentItemString) && ignoreEmptyResults)) result.Add(lastCurrentItemString); | ||
|
||
return result.ToArray(); | ||
} | ||
} | ||
} |