Changeset 5726 for icGREP


Ignore:
Timestamp:
Nov 6, 2017, 6:20:51 AM (18 months ago)
Author:
cameron
Message:

For lines ending CRLF, ensure that both bytes are written

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/grep_engine.cpp

    r5707 r5726  
    226226//  Default Report Match:  lines are emitted with whatever line terminators are found in the
    227227//  input.  However, if the final line is not terminated, a new line is appended.
     228//
     229//  It is possible that the line_end position is past the EOF, if there is an unterminated
     230//  final line.   To avoid potential bus errors, we only emit bytes up to but not
     231//  including the line_end position when we first find a match.
    228232void EmitMatch::accumulate_match (const size_t lineNum, char * line_start, char * line_end) {
    229233    if (!(WithFilenameFlag | LineNumberFlag) && (line_start == mPrevious_line_end + 1)) {
     
    233237    else {
    234238        if (mLineCount > 0) {
    235             // deal with the final byte of the previous line.
    236             mResultStr->write(mPrevious_line_end, 1);
     239            // Deal with the terminator of the previous line.  It could be an LF, the
     240            // last byte of NEL, LS or PS, or a two byte CRLF sequence.
     241            if (LLVM_UNLIKELY(mPrevious_line_end[0] == 0x0D)) {
     242                mResultStr->write(mPrevious_line_end, mPrevious_line_end[1] == 0x0A ? 2 : 1);
     243            }
     244            else {
     245                mResultStr->write(mPrevious_line_end, 1);
     246            }
    237247        }
    238248        if (WithFilenameFlag) {
     
    258268    if (mLineCount == 0) return;  // No matches.
    259269    if (mPrevious_line_end < buffer_end) {
    260         mResultStr->write(mPrevious_line_end, 1);
     270        if (LLVM_UNLIKELY(mPrevious_line_end[0] == 0x0D)) {
     271            mResultStr->write(mPrevious_line_end, mPrevious_line_end[1] == 0x0A ? 2 : 1);
     272        }
     273        else {
     274            mResultStr->write(mPrevious_line_end, 1);
     275        }
    261276    }
    262277    else {
Note: See TracChangeset for help on using the changeset viewer.