Skip to content

Commit

Permalink
Refactored findLine() Implementation
Browse files Browse the repository at this point in the history
Signed-off-by: yashTEF <yashsoc@gmail.com>
  • Loading branch information
yashTEF authored and datho7561 committed May 27, 2021
1 parent db84e94 commit 24569e0
Showing 1 changed file with 38 additions and 47 deletions.
Expand Up @@ -74,40 +74,21 @@ private int findLine(int offset) {

if (fLines.size() == 0) {
return -1;
<<<<<<< HEAD
}

int left = 0;
int right = fLines.size() - 1;
int mid = 0;
Line line = null;

while (left < right) {

mid = (left + right) / 2;

line = fLines.get(mid);
if (offset < line.offset) {
if (left == mid) {
right = left;
} else {
right = mid - 1;
}
} else if (offset > line.offset) {
if (right == mid) {
left = right;
} else {
left = mid + 1;
}
} else if (offset == line.offset) {
left = right = mid;
int right = fLines.size();
while (right - left > 1) {
int mid = (right + left) >> 1;
Line line = fLines.get(mid);
if (line.offset <= offset) {
left = mid;
} else {
right = mid;
}
}

line = fLines.get(left);
if (line.offset > offset) {
--left;
}
return left;
return (fLines.get(left).offset > offset) ? left - 1 : left;
>>>>>>> Refactored findLine() Implementation
}

@Override
Expand All @@ -127,13 +108,12 @@ public final Position getPositionAt(int offset) throws BadLocationException {
return new Position(lineNumber, character);
}


/**
* Returns the number of lines covered by the specified text range.
*
* @param startLine the line where the text range starts
* @param offset the start offset of the text range
* @param length the length of the text range
* @param offset the start offset of the text range
* @param length the length of the text range
* @return the number of lines covered by this text range
* @exception BadLocationException if range is undefined in this tracker
*/
Expand All @@ -143,9 +123,9 @@ private int getNumberOfLines(int startLine, int offset, int length) throws BadLo
return 1;
}

int target= offset + length;
int target = offset + length;

Line l= fLines.get(startLine);
Line l = fLines.get(startLine);

if (l.delimiter == null) {
return 1;
Expand All @@ -161,10 +141,10 @@ private int getNumberOfLines(int startLine, int offset, int length) throws BadLo

return getLineNumberOfOffset(target) - startLine + 1;
}

@Override
public final int getLineLength(int line) throws BadLocationException {
int lines= fLines.size();
int lines = fLines.size();

if (line < 0 || line > lines) {
throw new BadLocationException();
Expand All @@ -174,7 +154,7 @@ public final int getLineLength(int line) throws BadLocationException {
return 0;
}

Line l= fLines.get(line);
Line l = fLines.get(line);
return l.length;
}

Expand Down Expand Up @@ -237,6 +217,7 @@ public int getOffsetAt(Position position) throws BadLocationException {

@Override
public final Line getLineInformationOfOffset(int position) throws BadLocationException {
<<<<<<< HEAD
if (position > fTextLength) {
throw new BadLocationException("Offset > length: " + position + " > " + fTextLength); //$NON-NLS-1$//$NON-NLS-2$
}
Expand All @@ -247,12 +228,22 @@ public final Line getLineInformationOfOffset(int position) throws BadLocationExc
return new Line(0, 0);
}
Line l= fLines.get(size - 1);
=======
if (position > fTextLength)
throw new BadLocationException("Offset > length: " + position + " > " + fTextLength); //$NON-NLS-1$//$NON-NLS-2$

if (position == fTextLength) {
int size = fLines.size();
if (size == 0)
return new Line(0, 0);
Line l = fLines.get(size - 1);
>>>>>>> Refactored findLine() Implementation
return (l.delimiter != null ? new Line(fTextLength, 0) : new Line(fTextLength - l.length, l.length));
}

return getLineInformation(findLine(position));
}

@Override
public final Line getLineInformation(int line) throws BadLocationException {
int lines = fLines.size();
Expand Down Expand Up @@ -309,7 +300,7 @@ public final int getNumberOfLines() {
Line l = fLines.get(lines - 1);
return (l.delimiter != null ? lines + 1 : lines);
}

@Override
public final int getNumberOfLines(int position, int length) throws BadLocationException {

Expand All @@ -323,16 +314,16 @@ public final int getNumberOfLines(int position, int length) throws BadLocationEx

return getNumberOfLines(getLineNumberOfOffset(position), position, length);
}

@Override
public final int computeNumberOfLines(String text) {
int count= 0;
int start= 0;
DelimiterInfo delimiterInfo= nextDelimiterInfo(text, start);
int count = 0;
int start = 0;
DelimiterInfo delimiterInfo = nextDelimiterInfo(text, start);
while (delimiterInfo != null && delimiterInfo.delimiterIndex > -1) {
++count;
start= delimiterInfo.delimiterIndex + delimiterInfo.delimiterLength;
delimiterInfo= nextDelimiterInfo(text, start);
start = delimiterInfo.delimiterIndex + delimiterInfo.delimiterLength;
delimiterInfo = nextDelimiterInfo(text, start);
}
return count;
}
Expand Down Expand Up @@ -451,7 +442,7 @@ private int createLines(String text, int insertPosition, int offset) {
public final void replace(int position, int length, String text) throws BadLocationException {
throw new UnsupportedOperationException();
}

@Override
public final void set(String text) {
fLines.clear();
Expand Down

0 comments on commit 24569e0

Please sign in to comment.