22

What does "offset" mean in the context of programming?

Does it mean in the beginning or by a distance?

What does the String.offsetByCodePoints(int index, int codePointOffset) method do? What does "unpaired surrogates" in the method documentation mean?

approxiblue
  • 6,982
  • 16
  • 51
  • 59
skystar7
  • 4,419
  • 11
  • 38
  • 41
  • 3
    http://en.wikipedia.org/wiki/Offset_%28computer_science%29 for first part of your question – jmj Oct 19 '10 at 18:57
  • 1
    http://en.wikipedia.org/wiki/Mapping_of_Unicode_characters#Surrogates for the second part of your question – dsetton Oct 19 '10 at 19:01
  • The body of your question is not really conform to the title, because later you ask about a method in Java standard library. offset, is just the distance from origin for some axis, nothing more nothing less. – Curcuma_ Mar 17 '20 at 07:13

5 Answers5

5

What does "offset" mean in the context of programming? Does it mean in the beginning or by a distance?

In general, "offset" means some form of distance measured from some given position. The position could be the beginning of ... something ... but it isn't necessarily.

What "offset" specifically means will depend on the context in which it is used. (Ideally, the meaning will be evident from the context.)


What does the String.offsetByCodePoints(int index, int codePointOffset) method do?

This method calculates the position of a specific char within the String. The char will be the first char of the Unicode codepoint that is codePointOffset codepoints after the position given by index.

(So, in this context "offset" is referring a distance measured in Unicode code points from the position of a given code unit.)

Both index and the result are normal string index values; i.e. they are char positions.

The point ... is that when you are treating a String as sequence of Unicode codepoints, your code needs to take account of the fact that a codepoint may consist of either 1 or 2 char values.

To understand what the above all means, you may need to do some background reading on Unicode, codepoints and codeunits, and also on UTF-16 and how Java models Unicode strings.


What does "unpaired surrogates" in the method documentation mean?

Java strings represent characters that are Unicode code-points > 65535 as UTF-16 surrogate characters. In a well-formed UTF-16 string, the surrogates come in pairs, representing respectively the high and low order bits of the Unicode code-point.

The sentence is saying is that if a String contains surrogates that are not properly paired, it will treat them as separate codepoints ... for the purpose of counting code points.

See also: What is a "surrogate pair" in Java?

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
3

An example from wikipedia, let's say you have a string "abcdef" the 'd' character will have an offset of three starting from character 'a'.

Offset (computer science)

Sakén
  • 197
  • 1
  • 12
1

According to the JavaDoc,

String.offsetByCodePoints(int index, int codePointOffset)

Returns the index within this object that is offset from {@code index} by {@code codePointOffset} code points.

Here is an example of usage...

int num = 0;
num = "Test_String".offsetByCodePoints(0, 2); //num is 2
num = "Test_String".offsetByCodePoints(3, 2); //num is 5
num = "Test_String".offsetByCodePoints(9, 5); //Throws an exception since offset goes out-of-bounds
Ryan Berger
  • 9,644
  • 6
  • 44
  • 56
0

It's the distance 'travelled' to the address you wish to be at.

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 09 '22 at 15:24
-1

As per Wikipedia Offset (computer science)

An offset within an array or other data structure object is an integer indicating the distance (displacement) between the beginning of the object and a given element or point, presumably within the same object. The concept of a distance is valid only if all elements of the object are of the same size (typically given in bytes or words).

Get File Position

You can obtain the current position of a Java RandomAccessFile using its getFilePointer() method. The current position is the index (offset) of the byte that the RandomAccessFile is currently positioned at. Here is an example of obtaining the current position of a RandomAccessFile using its getFilePointer() method:

For example, let's take the Offest in the RandomAccessFile in java:

RandomAccessFile randomAccessFile = new RandomAccessFile("data/data.txt", "r");

 byte[] dest      = new byte[1024];
 int    offset    = 0;
 int    length    = 1024;
 int    bytesRead = randomAccessFile.read(dest, offset, length);

This example reads a sequence of bytes into the dest byte array passed as a parameter to the read() method. The read() method will start reading in the file from the current file position of the RandomAccessFile. The read() method will start writing data into the byte array starting from the array position provided by the offset parameter, and at most the number of bytes provided by the length parameter. This read() method returns the actual number of bytes read.

Maged Almaweri
  • 312
  • 4
  • 11