Libraryless. Click here for Pure Java version (2723L/18K).
// returns pair(character range, line) or null if not found // if no exact match found, return line above // nav takes a line and returns -1 (move up), 0 (found) or 1 (move down) static Pair<LongRange, S> binarySearchForLineInTextFile(File file, IF1<S, Int> nav) { long length = l(file); int bufSize = 1024; temp RandomAccessFile raf = randomAccessFileForReading(file); long min = 0, max = length; int direction = 0; Pair<LongRange, S> possibleResult = null; while ping (min < max) { long middle = (min+max)/2; long lineStart = raf_findBeginningOfLine(raf, middle, bufSize); long lineEnd = raf_findEndOfLine(raf, middle, bufSize); ifdef binarySearchForLineInTextFile_debug printVars_str("binsearch", +middle, +lineStart, +lineEnd); endifdef S line = fromUtf8(raf_readFilePart(raf, lineStart, (int) (lineEnd-1-lineStart))); direction = nav.get(line); possibleResult = pair(LongRange(lineStart, lineEnd), line); ifdef binarySearchForLineInTextFile_debug S _line = dropTrailingBackslashR(line); printVars_str("binsearch", +min, +max, +middle, +lineStart, +lineEnd, line := _line, +direction); endifdef if (direction == 0) ret possibleResult; // asserts are to assure that loop terminates if (direction < 0) max = assertLessThan(max, lineStart); else min = assertBiggerThan(min, lineEnd); ifdef binarySearchForLineInTextFile_debug printVars_str("binsearch", +min, +max); endifdef } ifdef binarySearchForLineInTextFile_debug printVars_str("binsearch ending", +direction); endifdef if (direction >= 0) ret possibleResult; long lineStart = raf_findBeginningOfLine(raf, min-1, bufSize); S line = fromUtf8(raf_readFilePart(raf, lineStart, (int) (min-1-lineStart))); ifdef binarySearchForLineInTextFile_debug printVars_str("binsearch end",+lineStart, +line); endifdef ret pair(LongRange(lineStart, min), line); }
download show line numbers debug dex old transpilations
Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1028341 |
Snippet name: | binarySearchForLineInTextFile |
Eternal ID of this version: | #1028341/17 |
Text MD5: | 9c7c1e37068d6da44d4018a852060399 |
Transpilation MD5: | 92829e6f376ff937d84d4c112991fdbe |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-06-13 01:38:56 |
Source code size: | 1980 bytes / 45 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 269 / 435 |
Version history: | 16 change(s) |
Referenced in: | [show references] |