1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | package Torello.Java; import Torello.Java.StringParse; import Torello.Java.StrPrint; import Torello.Java.StrIndexOf; import Torello.Java.Verbosity; import static Torello.Java.C.BYELLOW; import static Torello.Java.C.BCYAN; import static Torello.Java.C.BGREEN; import static Torello.Java.C.RESET; import java.io.IOException; import java.util.stream.Stream; // NOTE: Using 'final' everywhere here just helps remember that these are not being iterated, // changed or modified inside the For-Loops. It (sort of) helps "readability" (at least for me // anyway) class PrintingRecMultiLine { // Re-Use the Pointer, I guess private static final String I4 = Helper.I4; // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // Fields // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** final int matchPosStart; // Match Starting-Position final int matchPosEnd; // Match Ending-Position Array-Index final int matchLineNumStart; // Line-Number of First-Line of Match final int matchLineNumEnd; // Line-Number of Last-Line of Match final int startLineFirstCharPos; // String-index of First-Character of First-Line final int endLineLastCharPos; // String-index of Last-Character of Last-Line // NOTE: Presently, the start and end Column-Numbers are computed but ignored. // (These numbers are never printed, though that was the original plan). // Printing these two integers makes the output look just too verbose. final int matchStartCol; // Column-Number of First String-Index of Match final int matchEndCol; // Column-Number of Last String-Index of Match final String[] matchLines; // Lines of Text of the Match final String[] replaceLines; // Lines of Text of the Replacement final int[] matchLineLengths; // Length of each Line of Text of the Match final int[] replaceLineLengths; // Length of each Line of Text of the Replacement final String replaceStr; // Match Replacement String final int replaceEndLineNum; // Line-Number of Last-Line of Replacement final int replaceEndCol; // Column number of Last-Character of Replacement // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // Constructor // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** PrintingRecMultiLine( final String fileAsStr, final int matchPosStart, final int matchPosEnd, final String replaceStr, final PrintingRecMultiLine prevRec, final boolean useUNIXColors ) { // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // Basic Stuff: String-Index of Match-Start && String-Index of Match-End // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** this.matchPosStart = matchPosStart; this.matchPosEnd = matchPosEnd; this.replaceStr = replaceStr; // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // Line-Number containing the Match-Start && Line-Number containing the Match-End // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** /* System.out.println( "fileAsStr.substring: [" + fileAsStr.substring(matchPosStart, matchPosEnd) + "]\n" + "replaceStr: " + replaceStr ); */ // Initialize the Starting Line Number of the Match (Use StrPrint method) this.matchLineNumStart = (prevRec == null) ? StrPrint.lineNumber(fileAsStr, matchPosStart) : StrPrint.lineNumberSince( fileAsStr, matchPosStart, prevRec.matchLineNumEnd, prevRec.matchPosEnd ); // Initialize the Ending Line Number of the Match (also with StrPrint method) this.matchLineNumEnd = StrPrint.lineNumberSince (fileAsStr, matchPosEnd, this.matchLineNumStart, this.matchPosStart); /* System.out.println( "this.matchLineNumStart: " + this.matchLineNumStart + '\n' + "this.matchLineNumEnd: " + this.matchLineNumEnd ); */ // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // First-Char of First-Line, Last-Char of Last-Line // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // // These are **NOT** the same as matchPosStart & matchPosEnd // Loop-Variables, used below int i; final int FILE_LEN = fileAsStr.length(); // Compute the String[]-Array index for First-Charater in the First-Line of the Match for (i=matchPosStart; (i >= 0) && (fileAsStr.charAt(i) != '\n'); i--); this.startLineFirstCharPos = i + 1; // Compute the String[]-Array index for the Last-Charater in the Last-Line of the Match for (i=matchPosEnd; (i < FILE_LEN) && (fileAsStr.charAt(i) != '\n'); i++); this.endLineLastCharPos = i; // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // Match-Start String-Index Column && Match-End String-Index Column // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** this.matchStartCol = this.matchPosStart - this.startLineFirstCharPos; this.matchEndCol = this.endLineLastCharPos - this.matchPosEnd; // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // Lines of Text of the Match, itself // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** final String _BCYAN = useUNIXColors ? BCYAN : ""; final String _BGREEN = useUNIXColors ? BGREEN : ""; final String _RESET = useUNIXColors ? RESET : ""; final String preStr = fileAsStr.substring(this.startLineFirstCharPos, matchPosStart); final String matchStr = fileAsStr.substring(matchPosStart, matchPosEnd); final String postStr = fileAsStr.substring(matchPosEnd, this.endLineLastCharPos); // The list of newline characters inside the Match-String int[] posArr = StrIndexOf.all(matchStr, '\n'); // Build a String[]-Array of the Lines that comprise the Match-String Stream.Builder<String> linesBuilder = Stream.builder(); for (i=0; i < posArr.length; i++) linesBuilder.accept( ((i == 0) ? preStr : "") + _BGREEN + matchStr.substring(((i == 0) ? 0 : (posArr[i-1] + 1)), posArr[i]) + RESET // ((i == (posArr.length - 1)) ? (RESET + postStr) : "") ); // An **ENDING-READ** that makes sure to insert the last-section after the last '\n' linesBuilder.accept( ((i == 0) ? preStr : "") + _BGREEN + matchStr.substring(((i == 0) ? 0 : (posArr[i-1] + 1)), matchStr.length()) + RESET + postStr ); this.matchLines = linesBuilder.build().toArray(String[]::new); // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // Lines of Text of the Replaceent // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // The list of newlne characters inside the Replacement-String posArr = StrIndexOf.all(replaceStr, '\n'); // Build a String[]-Array of the Lines that comprise the Replacement-String linesBuilder = Stream.builder(); for (i=0; i < posArr.length; i++) linesBuilder.accept( ((i == 0) ? preStr : "") + _BCYAN + replaceStr.substring(((i == 0) ? 0 : (posArr[i-1] + 1)), posArr[i]) + RESET ); // An **ENDING-READ** that makes sure to insert the last-section after the last '\n' linesBuilder.accept( ((i == 0) ? preStr : "") + _BCYAN + replaceStr.substring(((i == 0) ? 0 : (posArr[i-1] + 1)), replaceStr.length()) + RESET + postStr ); this.replaceLines = linesBuilder.build().toArray(String[]::new); // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // Match Line-Lengths Array // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // There is an additional '+1' because of the '\n' **ITSELF**. If you include the '\n' // in the Line-Length Computation, the length of the line will be identical to what the // Text-Editor says about the Line-Length. // // The rule of "Line-Length Max 100" (no more than 100 characters in a line), is // consistent with what the Text-Editor says about the Line-Length. Again, the Text // Editor would say that the Line-Length includes the '\n' final int OTHER = 1 - _BGREEN.length() - _RESET.length(); this.matchLineLengths = new int[this.matchLines.length]; for (i=0; i < this.matchLines.length; i++) this.matchLineLengths[i] = this.matchLines[i].length() + OTHER; // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // Replacement Line-Lengths Array // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** this.replaceLineLengths = new int[this.replaceLines.length]; for (i=0; i < this.replaceLines.length; i++) this.replaceLineLengths[i] = this.replaceLines[i].length() + OTHER; // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // Replacement-String Character-Stuff // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // final int replaceEndLineNum; // Line-Number of Last-Line of Replacement this.replaceEndLineNum = this.matchLineNumStart + this.replaceLines.length; // final int replaceEndCol; // Column number of Last-Character of Replacement this.replaceEndCol = this.replaceLines[this.replaceLines.length - 1].length(); } // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // Print All // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** static void printAll( final PrintingRecMultiLine[] recs, final Appendable appendable, final boolean useUNIXColors, final Verbosity verbosity ) // This method cannot actually throw IOException. throws IOException { // '.....' is printed when the user requests Verbosity.Quiet mode. Therefore in this // particular case, it is necessary to print a newline character. if (verbosity == Verbosity.Quiet) appendable.append('\n'); int MAX_LINE_LEN = 0; int MAX_LINE_NUM = 0; int MAX_COL = 0; for (PrintingRecMultiLine r : recs) { for (int i : r.matchLineLengths) if (i > MAX_LINE_LEN) MAX_LINE_LEN = i; for (int i : r.replaceLineLengths) if (i > MAX_LINE_LEN) MAX_LINE_LEN = i; if (r.matchLineNumEnd > MAX_LINE_NUM) MAX_LINE_NUM = r.matchLineNumEnd; if (r.replaceEndLineNum > MAX_LINE_NUM) MAX_LINE_NUM = r.replaceEndLineNum; // System.out.println("r.matchLineNumEnd: " + r.matchLineNumEnd); // System.out.println("r.replaceEndLineNum: " + r.replaceEndLineNum); if (r.matchEndCol > MAX_COL) MAX_COL = r.matchEndCol; if (r.replaceEndCol > MAX_COL) MAX_COL = r.replaceEndCol; } final int LINE_LEN_CHARS = 1 + ((int) Math.floor(Math.log10(MAX_LINE_LEN))); final int LINE_NUM_CHARS = 1 + ((int) Math.floor(Math.log10(MAX_LINE_NUM))); final int COL_CHARS = 1 + ((int) Math.floor(Math.log10(MAX_COL))); StringBuilder sb = new StringBuilder(); int matchNum = 1; for (PrintingRecMultiLine r : recs) { sb.append("Original Text, Match #" + matchNum + ":\n"); int lineNum = r.matchLineNumStart; for (int i=0; i < r.matchLines.length; i++) { String lineNumStr = "" + (lineNum++); lineNumStr += StringParse.nChars(' ', LINE_NUM_CHARS - lineNumStr.length()); String lineLenStr = "" + r.matchLineLengths[i]; lineLenStr += StringParse.nChars(' ', LINE_LEN_CHARS - lineLenStr.length()); if (useUNIXColors) { lineNumStr = BYELLOW + lineNumStr + RESET; lineLenStr = BYELLOW + lineLenStr + RESET; } sb.append( I4 + "Line #: " + lineNumStr + ' ' + "Len: " + lineLenStr + ' ' + r.matchLines[i] + '\n' ); } sb.append("Updated Text, Match #" + matchNum + ":\n"); lineNum = r.matchLineNumStart; for (int i=0; i < r.replaceLines.length; i++) { String lineNumStr = "" + (lineNum++); lineNumStr += StringParse.nChars(' ', LINE_NUM_CHARS - lineNumStr.length()); String lineLenStr = "" + r.replaceLineLengths[i]; lineLenStr += StringParse.nChars(' ', LINE_LEN_CHARS - lineLenStr.length()); if (useUNIXColors) { lineNumStr = BYELLOW + lineNumStr + RESET; lineLenStr = BYELLOW + lineLenStr + RESET; } sb.append( I4 + "Line #: " + lineNumStr + ' ' + "Len: " + lineLenStr + ' ' + r.replaceLines[i] + '\n' ); } sb.append('\n'); matchNum++; } appendable.append(sb.toString()); } } |