6cbc99793f0122271f72223a9e582d3829a17d7be95ac48f75d7da292c6f55d4c6233fc876496a270d17b9d14f4c92dc10308baecbfc318a42a9e89fabb58b 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. export function countEOL(text) {
  6. let eolCount = 0;
  7. let firstLineLength = 0;
  8. let lastLineStart = 0;
  9. let eol = 0 /* StringEOL.Unknown */;
  10. for (let i = 0, len = text.length; i < len; i++) {
  11. const chr = text.charCodeAt(i);
  12. if (chr === 13 /* CharCode.CarriageReturn */) {
  13. if (eolCount === 0) {
  14. firstLineLength = i;
  15. }
  16. eolCount++;
  17. if (i + 1 < len && text.charCodeAt(i + 1) === 10 /* CharCode.LineFeed */) {
  18. // \r\n... case
  19. eol |= 2 /* StringEOL.CRLF */;
  20. i++; // skip \n
  21. }
  22. else {
  23. // \r... case
  24. eol |= 3 /* StringEOL.Invalid */;
  25. }
  26. lastLineStart = i + 1;
  27. }
  28. else if (chr === 10 /* CharCode.LineFeed */) {
  29. // \n... case
  30. eol |= 1 /* StringEOL.LF */;
  31. if (eolCount === 0) {
  32. firstLineLength = i;
  33. }
  34. eolCount++;
  35. lastLineStart = i + 1;
  36. }
  37. }
  38. if (eolCount === 0) {
  39. firstLineLength = text.length;
  40. }
  41. return [eolCount, firstLineLength, text.length - lastLineStart, eol];
  42. }