StringSplitter.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Client.SSE
  7. {
  8. public class StringSplitter
  9. {
  10. public static string[] SplitIntoLines(string text, out string remainingText)
  11. {
  12. List<string> lines = new List<string>();
  13. //bool endFound = false;
  14. //bool searchingForFirstChar = true;
  15. int lineLength = 0;
  16. char previous = char.MinValue;
  17. for (int i = 0; i < text.Length; i++)
  18. {
  19. char c = text[i];
  20. if (c == '\n' || c == '\r')
  21. {
  22. bool isCRLFPair = previous=='\r' && c == '\n';
  23. if (!isCRLFPair)
  24. {
  25. string line = text.Substring(i - lineLength, lineLength);
  26. lines.Add(line);
  27. }
  28. lineLength = 0;
  29. }
  30. else
  31. {
  32. lineLength++;
  33. }
  34. previous = c;
  35. }
  36. // Save the last chars that is not followed by a lineending.
  37. remainingText = text.Substring(text.Length - lineLength);
  38. return lines.ToArray();
  39. }
  40. }
  41. }