Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cs to vb trivia #517

Merged
merged 16 commits into from
Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Consolidate code to avoid duplication
  • Loading branch information
GrahamTheCoder committed Feb 3, 2020
commit de6ac63b2a38d6d40615d4a2d70847d146d91dbe
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
using System.Collections.Generic;
using System.Text;

namespace CodeConverter.Tests
namespace CodeConverter.Util
{
#if NR6
public
#endif
internal enum UnicodeNewline
{
Unknown,
Expand Down Expand Up @@ -309,6 +306,34 @@ public static bool IsNewLine(char ch)
ch == NewLine.FF ||
ch == NewLine.LS ||
ch == NewLine.PS;
}

/// <summary>
/// Determines if a string is a new line delimiter.
///
/// Note that the only 2 char wide new line is CR LF
/// </summary>
public static bool IsNewLine(this string str)
{
if (str == null || str.Length == 0) {
return false;
}
char ch = str[0];
var switchExpr = str.Length;
switch (switchExpr) {
case 0: {
return false;
}

case 1:
case 2: {
return ch == CR || ch == LF || ch == NEL || ch == VT || ch == FF || ch == LS || ch == PS;
}

default: {
return false;
}
}
}

/// <summary>
Expand Down Expand Up @@ -363,6 +388,32 @@ public static string[] SplitLines(string text)

return result.ToArray();
}

/// <summary>
/// Replace Unicode NewLines with ControlChars.NullChar or Specified Character
/// </summary>
/// <param name="text">Source Test</param>
/// <param name="SubstituteChar">Default is vbNullChar</param>
/// <returns>String with Unicode NewLines replaced with SubstituteChar</returns>
public static string WithoutNewLines(this string text, char SubstituteChar = default)
{
System.Diagnostics.Contracts.Contract.Requires(text != null);
var sb = new StringBuilder();
int length = default(int);
UnicodeNewline type = default(UnicodeNewline);

for (int i = 0, loopTo = text.Length - 1; i <= loopTo; i++) {
char ch = text[i];
// Do not delete the next line
int j = i;
if (TryGetDelimiterLengthAndType(ch, out length, out type, () => j < text.Length - 1 ? text[j + 1] : SubstituteChar)) {
i += length - 1;
continue;
}
sb.Append(ch);
}
return sb.ToString();
}
}
}

Loading