Home » Archived » IMP » LexStream
LexStream [message #5352] |
Thu, 20 September 2007 16:04  |
Eclipse User |
|
|
|
Originally posted by: olegm.gmail.com
Hello,
Class LexStream provides the following methods
-----------------------
class LexStream {
public int getLineOffset(int i) { return lineOffsets.get(i); }
public int getLineNumberOfCharAt(int i)
{
int index = lineOffsets.binarySearch(i);
return index < 0 ? -index : index == 0 ? 1 : index;
}
}
-----------------------
It seems that the former works with 0-based line indexes, while the
latter works with 1-based, or to put it another way:
getLineOffset(getLineNumberOfCharAt(0)) > 0
This is because of the way the binary search is defined:
-------------
public class IntSegmentedTuple
{
// If array is sorted, this function will find the index location
// of a given element if it is contained in the array. Otherwise, it
// will return the negation of the index of the element prior to
// which the new element would be inserted in the array.
//
public int binarySearch(int element) {
...
}
}
-------------
Is this behavior intentional?
Best,
OM
|
|
|
Re: LexStream [message #7318 is a reply to message #5352] |
Fri, 21 September 2007 12:20   |
Eclipse User |
|
|
|
Oleg,
I am sure that this is intentional, but Philippe will have to explain
the rationale.
When you say that getLineOffset(int i) works with 0-based line indexes,
what do you mean? As I read the code, the value of getLineOffset(0)
will always be -1. So, you can call getLineOffset with 0, but you will
never get a meaningful value back from that. (I.e., as I interpret it,
(getLineOffset(0) == -1) => line 0 is not in the file.) Also,
getLineOffset(1) will tell you the end of the first line in the file
(and so on for subsequent lines). So it seems to me that
getLineOffset(int) in effect starts counting real lines in the file at
1, not 0. (Philippe can correct me if I'm misinterpreting.)
I don't think it's necessarily true that
getLineOffset(getLineNumberOfCharAt(0)) > 0
That is, getLineNumberOfCharAt(0) should return 1 in any case, but I
believe that getLineOffset(1) can be 0 if the first line is empty.
(Again, Philippe can correct me if I'm wrong.)
Anyway, does this cause a specific bug for you, or are you just trying
to make sense of the code?
Regards,
Stan
Oleg Murk wrote:
> Hello,
>
> Class LexStream provides the following methods
> -----------------------
> class LexStream {
> public int getLineOffset(int i) { return lineOffsets.get(i); }
>
> public int getLineNumberOfCharAt(int i)
> {
> int index = lineOffsets.binarySearch(i);
> return index < 0 ? -index : index == 0 ? 1 : index;
> }
> }
> -----------------------
> It seems that the former works with 0-based line indexes, while the
> latter works with 1-based, or to put it another way:
> getLineOffset(getLineNumberOfCharAt(0)) > 0
> This is because of the way the binary search is defined:
> -------------
> public class IntSegmentedTuple
> {
> // If array is sorted, this function will find the index location
> // of a given element if it is contained in the array. Otherwise, it
> // will return the negation of the index of the element prior to
> // which the new element would be inserted in the array.
> //
> public int binarySearch(int element) {
> ...
> }
> }
> -------------
> Is this behavior intentional?
>
> Best,
> OM
|
|
|
Re: LexStream [message #7327 is a reply to message #7318] |
Fri, 21 September 2007 12:41   |
Eclipse User |
|
|
|
Originally posted by: olegm.gmail.com
Hello,
Stanley Sutton wrote:
> When you say that getLineOffset(int i) works with 0-based line indexes,
> what do you mean? As I read the code, the value of getLineOffset(0)
> will always be -1. So, you can call getLineOffset with 0, but you will
> never get a meaningful value back from that. (I.e., as I interpret it,
> (getLineOffset(0) == -1) => line 0 is not in the file.) Also,
> getLineOffset(1) will tell you the end of the first line in the file
> (and so on for subsequent lines). So it seems to me that
> getLineOffset(int) in effect starts counting real lines in the file at
> 1, not 0. (Philippe can correct me if I'm misinterpreting.)
First, the question is purely aesthetical. I can always substract 1 if
needed. Your reading is that "getLineOffset" returns the offset of the
end of the line, while from the method name I personally concluded that
what I get is the offset of the beginning of the line. Also I wouldn't
say that "getLineOffset(0) -> -1" should never be called as it is needed
to convert from (line, column) to offsets. Of course there wouldn't be a
problem if ILexStream had JavaDoc :)
Best,
OM
|
|
|
Re: LexStream [message #7338 is a reply to message #5352] |
Fri, 21 September 2007 14:33  |
Eclipse User |
|
|
|
Yes, the behavior is intentional.
For a given line number i, i > 0, the method getLineOffset(i) returns
the offset of the newline character that ends the line in question.
getLineOffset(0) returns -1.
Given an offset i in the input, getLineNumberOfCharAt(i) returns the
line number that contains offset i where (as you noticed) the lines are
numbered starting at 1. Hence, 1 will always be the lowest line number
returned. In particular, for i <= 0, getLineNumberOfCharAt(i) returns 1.
Oleg Murk wrote:
> Hello,
>
> Class LexStream provides the following methods
> -----------------------
> class LexStream {
> public int getLineOffset(int i) { return lineOffsets.get(i); }
>
> public int getLineNumberOfCharAt(int i)
> {
> int index = lineOffsets.binarySearch(i);
> return index < 0 ? -index : index == 0 ? 1 : index;
> }
> }
> -----------------------
> It seems that the former works with 0-based line indexes, while the
> latter works with 1-based, or to put it another way:
> getLineOffset(getLineNumberOfCharAt(0)) > 0
> This is because of the way the binary search is defined:
> -------------
> public class IntSegmentedTuple
> {
> // If array is sorted, this function will find the index location
> // of a given element if it is contained in the array. Otherwise, it
> // will return the negation of the index of the element prior to
> // which the new element would be inserted in the array.
> //
> public int binarySearch(int element) {
> ...
> }
> }
> -------------
> Is this behavior intentional?
>
> Best,
> OM
|
|
|
Re: LexStream [message #566539 is a reply to message #5352] |
Fri, 21 September 2007 12:20  |
Eclipse User |
|
|
|
Oleg,
I am sure that this is intentional, but Philippe will have to explain
the rationale.
When you say that getLineOffset(int i) works with 0-based line indexes,
what do you mean? As I read the code, the value of getLineOffset(0)
will always be -1. So, you can call getLineOffset with 0, but you will
never get a meaningful value back from that. (I.e., as I interpret it,
(getLineOffset(0) == -1) => line 0 is not in the file.) Also,
getLineOffset(1) will tell you the end of the first line in the file
(and so on for subsequent lines). So it seems to me that
getLineOffset(int) in effect starts counting real lines in the file at
1, not 0. (Philippe can correct me if I'm misinterpreting.)
I don't think it's necessarily true that
getLineOffset(getLineNumberOfCharAt(0)) > 0
That is, getLineNumberOfCharAt(0) should return 1 in any case, but I
believe that getLineOffset(1) can be 0 if the first line is empty.
(Again, Philippe can correct me if I'm wrong.)
Anyway, does this cause a specific bug for you, or are you just trying
to make sense of the code?
Regards,
Stan
Oleg Murk wrote:
> Hello,
>
> Class LexStream provides the following methods
> -----------------------
> class LexStream {
> public int getLineOffset(int i) { return lineOffsets.get(i); }
>
> public int getLineNumberOfCharAt(int i)
> {
> int index = lineOffsets.binarySearch(i);
> return index < 0 ? -index : index == 0 ? 1 : index;
> }
> }
> -----------------------
> It seems that the former works with 0-based line indexes, while the
> latter works with 1-based, or to put it another way:
> getLineOffset(getLineNumberOfCharAt(0)) > 0
> This is because of the way the binary search is defined:
> -------------
> public class IntSegmentedTuple
> {
> // If array is sorted, this function will find the index location
> // of a given element if it is contained in the array. Otherwise, it
> // will return the negation of the index of the element prior to
> // which the new element would be inserted in the array.
> //
> public int binarySearch(int element) {
> ...
> }
> }
> -------------
> Is this behavior intentional?
>
> Best,
> OM
|
|
|
Re: LexStream [message #566575 is a reply to message #7318] |
Fri, 21 September 2007 12:41  |
Eclipse User |
|
|
|
Originally posted by: olegm.gmail.com
Hello,
Stanley Sutton wrote:
> When you say that getLineOffset(int i) works with 0-based line indexes,
> what do you mean? As I read the code, the value of getLineOffset(0)
> will always be -1. So, you can call getLineOffset with 0, but you will
> never get a meaningful value back from that. (I.e., as I interpret it,
> (getLineOffset(0) == -1) => line 0 is not in the file.) Also,
> getLineOffset(1) will tell you the end of the first line in the file
> (and so on for subsequent lines). So it seems to me that
> getLineOffset(int) in effect starts counting real lines in the file at
> 1, not 0. (Philippe can correct me if I'm misinterpreting.)
First, the question is purely aesthetical. I can always substract 1 if
needed. Your reading is that "getLineOffset" returns the offset of the
end of the line, while from the method name I personally concluded that
what I get is the offset of the beginning of the line. Also I wouldn't
say that "getLineOffset(0) -> -1" should never be called as it is needed
to convert from (line, column) to offsets. Of course there wouldn't be a
problem if ILexStream had JavaDoc :)
Best,
OM
|
|
|
Re: LexStream [message #566597 is a reply to message #5352] |
Fri, 21 September 2007 14:33  |
Eclipse User |
|
|
|
Yes, the behavior is intentional.
For a given line number i, i > 0, the method getLineOffset(i) returns
the offset of the newline character that ends the line in question.
getLineOffset(0) returns -1.
Given an offset i in the input, getLineNumberOfCharAt(i) returns the
line number that contains offset i where (as you noticed) the lines are
numbered starting at 1. Hence, 1 will always be the lowest line number
returned. In particular, for i <= 0, getLineNumberOfCharAt(i) returns 1.
Oleg Murk wrote:
> Hello,
>
> Class LexStream provides the following methods
> -----------------------
> class LexStream {
> public int getLineOffset(int i) { return lineOffsets.get(i); }
>
> public int getLineNumberOfCharAt(int i)
> {
> int index = lineOffsets.binarySearch(i);
> return index < 0 ? -index : index == 0 ? 1 : index;
> }
> }
> -----------------------
> It seems that the former works with 0-based line indexes, while the
> latter works with 1-based, or to put it another way:
> getLineOffset(getLineNumberOfCharAt(0)) > 0
> This is because of the way the binary search is defined:
> -------------
> public class IntSegmentedTuple
> {
> // If array is sorted, this function will find the index location
> // of a given element if it is contained in the array. Otherwise, it
> // will return the negation of the index of the element prior to
> // which the new element would be inserted in the array.
> //
> public int binarySearch(int element) {
> ...
> }
> }
> -------------
> Is this behavior intentional?
>
> Best,
> OM
|
|
|
Goto Forum:
Current Time: Sun May 11 09:04:43 EDT 2025
Powered by FUDForum. Page generated in 0.03915 seconds
|