ascii.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { compareRangeCovs } from "./compare";
  2. import { RangeCov } from "./types";
  3. interface ReadonlyRangeTree {
  4. readonly start: number;
  5. readonly end: number;
  6. readonly count: number;
  7. readonly children: ReadonlyRangeTree[];
  8. }
  9. export function emitForest(trees: ReadonlyArray<ReadonlyRangeTree>): string {
  10. return emitForestLines(trees).join("\n");
  11. }
  12. export function emitForestLines(trees: ReadonlyArray<ReadonlyRangeTree>): string[] {
  13. const colMap: Map<number, number> = getColMap(trees);
  14. const header: string = emitOffsets(colMap);
  15. return [header, ...trees.map(tree => emitTree(tree, colMap).join("\n"))];
  16. }
  17. function getColMap(trees: Iterable<ReadonlyRangeTree>): Map<number, number> {
  18. const eventSet: Set<number> = new Set();
  19. for (const tree of trees) {
  20. const stack: ReadonlyRangeTree[] = [tree];
  21. while (stack.length > 0) {
  22. const cur: ReadonlyRangeTree = stack.pop()!;
  23. eventSet.add(cur.start);
  24. eventSet.add(cur.end);
  25. for (const child of cur.children) {
  26. stack.push(child);
  27. }
  28. }
  29. }
  30. const events: number[] = [...eventSet];
  31. events.sort((a, b) => a - b);
  32. let maxDigits: number = 1;
  33. for (const event of events) {
  34. maxDigits = Math.max(maxDigits, event.toString(10).length);
  35. }
  36. const colWidth: number = maxDigits + 3;
  37. const colMap: Map<number, number> = new Map();
  38. for (const [i, event] of events.entries()) {
  39. colMap.set(event, i * colWidth);
  40. }
  41. return colMap;
  42. }
  43. function emitTree(tree: ReadonlyRangeTree, colMap: Map<number, number>): string[] {
  44. const layers: ReadonlyRangeTree[][] = [];
  45. let nextLayer: ReadonlyRangeTree[] = [tree];
  46. while (nextLayer.length > 0) {
  47. const layer: ReadonlyRangeTree[] = nextLayer;
  48. layers.push(layer);
  49. nextLayer = [];
  50. for (const node of layer) {
  51. for (const child of node.children) {
  52. nextLayer.push(child);
  53. }
  54. }
  55. }
  56. return layers.map(layer => emitTreeLayer(layer, colMap));
  57. }
  58. export function parseFunctionRanges(text: string, offsetMap: Map<number, number>): RangeCov[] {
  59. const result: RangeCov[] = [];
  60. for (const line of text.split("\n")) {
  61. for (const range of parseTreeLayer(line, offsetMap)) {
  62. result.push(range);
  63. }
  64. }
  65. result.sort(compareRangeCovs);
  66. return result;
  67. }
  68. /**
  69. *
  70. * @param layer Sorted list of disjoint trees.
  71. * @param colMap
  72. */
  73. function emitTreeLayer(layer: ReadonlyRangeTree[], colMap: Map<number, number>): string {
  74. const line: string[] = [];
  75. let curIdx: number = 0;
  76. for (const {start, end, count} of layer) {
  77. const startIdx: number = colMap.get(start)!;
  78. const endIdx: number = colMap.get(end)!;
  79. if (startIdx > curIdx) {
  80. line.push(" ".repeat(startIdx - curIdx));
  81. }
  82. line.push(emitRange(count, endIdx - startIdx));
  83. curIdx = endIdx;
  84. }
  85. return line.join("");
  86. }
  87. function parseTreeLayer(text: string, offsetMap: Map<number, number>): RangeCov[] {
  88. const result: RangeCov[] = [];
  89. const regex: RegExp = /\[(\d+)-*\)/gs;
  90. while (true) {
  91. const match: RegExpMatchArray | null = regex.exec(text);
  92. if (match === null) {
  93. break;
  94. }
  95. const startIdx: number = match.index!;
  96. const endIdx: number = startIdx + match[0].length;
  97. const count: number = parseInt(match[1], 10);
  98. const startOffset: number | undefined = offsetMap.get(startIdx);
  99. const endOffset: number | undefined = offsetMap.get(endIdx);
  100. if (startOffset === undefined || endOffset === undefined) {
  101. throw new Error(`Invalid offsets for: ${JSON.stringify(text)}`);
  102. }
  103. result.push({startOffset, endOffset, count});
  104. }
  105. return result;
  106. }
  107. function emitRange(count: number, len: number): string {
  108. const rangeStart: string = `[${count.toString(10)}`;
  109. const rangeEnd: string = ")";
  110. const hyphensLen: number = len - (rangeStart.length + rangeEnd.length);
  111. const hyphens: string = "-".repeat(Math.max(0, hyphensLen));
  112. return `${rangeStart}${hyphens}${rangeEnd}`;
  113. }
  114. function emitOffsets(colMap: Map<number, number>): string {
  115. let line: string = "";
  116. for (const [event, col] of colMap) {
  117. if (line.length < col) {
  118. line += " ".repeat(col - line.length);
  119. }
  120. line += event.toString(10);
  121. }
  122. return line;
  123. }
  124. export function parseOffsets(text: string): Map<number, number> {
  125. const result: Map<number, number> = new Map();
  126. const regex: RegExp = /\d+/gs;
  127. while (true) {
  128. const match: RegExpExecArray | null = regex.exec(text);
  129. if (match === null) {
  130. break;
  131. }
  132. result.set(match.index, parseInt(match[0], 10));
  133. }
  134. return result;
  135. }