Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Xunit.Sdk;
- namespace SubtitleEditTest;
- public class UnitTest1
- {
- [Theory]
- [InlineData("foobar \r\nfoobar")]
- [InlineData("foobar\r\n foobar")]
- [InlineData("foobar \r\n foobar")]
- [InlineData("foobar\r\nfoobar")]
- public void RemoveWhiteSpaceAfterLineBreakTest(string input)
- {
- const string expected = "foobar\r\nfoobar";
- var outputChars = RemoveWhiteSpaceAfterLineBreak(input);
- Assert.Equal(expected, outputChars);
- }
- [Theory]
- [InlineData(" ")]
- public void RemoveWhiteSpaceAfterLineBreakTestExpectingWhiteSpace(string input)
- {
- const string expected = " ";
- var outputChars = RemoveWhiteSpaceAfterLineBreak(input);
- Assert.Equal(expected, outputChars);
- }
- [Theory]
- [InlineData("f o")]
- [InlineData("f o")]
- public void FixExtraSpacesTest(string s)
- {
- var outputChars = FixExtraSpaces(s);
- Assert.Equal("f o", outputChars);
- }
- [Theory]
- [InlineData("o ")]
- [InlineData("o ")]
- public void FixExtraSpacesTrailingTest(string s)
- {
- var outputChars = FixExtraSpaces(s);
- Assert.Equal("o ", outputChars);
- }
- [Theory]
- [InlineData("o . ")]
- public void FixExtraSpacesDotTest(string s)
- {
- var outputChars = FixExtraSpaces(s);
- Assert.Equal("o . ", outputChars);
- }
- [Theory]
- [InlineData(" o")]
- public void FixExtraSpacesLeadingTest(string s)
- {
- var outputChars = FixExtraSpaces(s);
- Assert.Equal(" o", outputChars);
- }
- [Theory]
- [InlineData("foobar !")]
- [InlineData("foobar !")]
- public void FixExtraSpacesCharAfter(string s)
- {
- var outputChars = FixExtraSpaces(s);
- Assert.Equal("foobar !", outputChars);
- }
- [Theory]
- [InlineData("foobar \r\n ")]
- [InlineData("foobar \r\n")]
- public void FixExtraSpacesCharNewLine(string s)
- {
- var outputChars = FixExtraSpaces(s);
- Assert.Equal("foobar\r\n", outputChars);
- }
- private static string FixExtraSpaces(string s)
- {
- if (string.IsNullOrEmpty(s))
- {
- return s;
- }
- var lineBreakPositionTrack = -1;
- var whiteSpaceTrack = -1; // track last white space position
- var writeIndex = s.Length - 1; // track current slow available for writing
- var charBuffer = new char[s.Length]; // array storing the outputChars text
- // if current is letter or symbols take and rest track position
- // if current whitespace and no track take and track position
- // if pre was whitespace and current is \r or \n => replace white-space
- for (int i = s.Length - 1; i >= 0; i--)
- {
- var ch = s[i];
- if (ch == ' ')
- {
- // if condition is false then ignore the white space
- if (whiteSpaceTrack >= 0 || lineBreakPositionTrack >= 0)
- {
- continue; // ignore white space
- }
- // if last known char beside white space was \r or \n then we want to ignore the white space
- whiteSpaceTrack = writeIndex;
- // write the white space index and decrement to next position
- charBuffer[writeIndex--] = ch;
- }
- else if (ch == '\n' || ch == '\r')
- {
- // if white space tracking (whiteSpaceTrack) variable has value then
- // then that means out last write was a white space and now we want to override the value with \r or \n
- writeIndex = whiteSpaceTrack > 0 ? whiteSpaceTrack : writeIndex;
- // track the last line breaking position
- lineBreakPositionTrack = writeIndex;
- // write line breaking chars and decrement to next position
- charBuffer[writeIndex--] = ch;
- // reset the white space tracking variable
- whiteSpaceTrack = -1;
- }
- else // handle none whitespace and new line chars
- {
- // white currently non-white space char and decrement the writing tracking variable
- charBuffer[writeIndex--] = ch;
- // reset the white space tracking index
- whiteSpaceTrack = -1;
- }
- }
- return new string(charBuffer, writeIndex + 1, charBuffer.Length - (writeIndex + 1));
- }
- private static string RemoveWhiteSpaceAfterLineBreak(string input)
- {
- if (!input.Contains(Environment.NewLine))
- {
- return input;
- }
- var inputLen = input.Length;
- var inputCharSlotTrack = inputLen - 1;
- var inputChars = new char[inputLen];
- for (int i = inputLen - 1; i >= 0; i--)
- {
- var ch = input[i];
- if (ch != ' ' || (!IsLineBreakFollowOrHeading(i - 1) && !IsLineBreakFollowOrHeading(i + 1)))
- {
- inputChars[inputCharSlotTrack--] = ch;
- }
- }
- return new string(inputChars, inputCharSlotTrack + 1, inputLen - (inputCharSlotTrack + 1));
- bool IsKnownLineBreakChar(char ch) => ch == '\n' || ch == '\r';
- bool IsLineBreakFollowOrHeading(int charIndex) =>
- charIndex >= 0 && charIndex < input.Length && IsKnownLineBreakChar(input[charIndex]);
- }
- }
Add Comment
Please, Sign In to add comment