Skip to content

CSV-196: Comments changes on Dec30 #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/main/java/org/apache/commons/csv/CSVParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ public Builder setRecordNumber(final long recordNumber) {
*
* @param enableByteTracking {@code true} to enable byte tracking; {@code false} to disable it.
* @return this instance.
* @since 1.13.0
*/
public Builder setEnableByteTracking(final boolean enableByteTracking) {
this.enableByteTracking = enableByteTracking;
Expand Down Expand Up @@ -885,7 +886,7 @@ CSVRecord nextRecord() throws IOException {
recordList.clear();
StringBuilder sb = null;
final long startCharPosition = lexer.getCharacterPosition() + characterOffset;
final long startCharByte = lexer.getBytesRead() + this.characterOffset;
final long startBytePosition = lexer.getBytesRead() + this.characterOffset;
do {
reusableToken.reset();
lexer.nextToken(reusableToken);
Expand Down Expand Up @@ -923,7 +924,7 @@ CSVRecord nextRecord() throws IOException {
recordNumber++;
final String comment = Objects.toString(sb, null);
result = new CSVRecord(this, recordList.toArray(Constants.EMPTY_STRING_ARRAY), comment,
recordNumber, startCharPosition, startCharByte);
recordNumber, startCharPosition, startBytePosition);
}
return result;
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/apache/commons/csv/CSVRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public final class CSVRecord implements Serializable, Iterable<String> {
private final long characterPosition;

/**
* The start byte of this record as a character byte in the source stream.
* The starting position of this record in the source stream, measured in bytes.
*/
private final long bytePosition;

Expand Down Expand Up @@ -152,9 +152,10 @@ public long getCharacterPosition() {
}

/**
* Gets the start byte of this record as a character byte in the source stream
* Returns the starting position of this record in the source stream, measured in bytes.
*
* @return the start byte of this record as a character byte in the source stream.
* @return the byte position of this record in the source stream.
* @since 1.13.0
*/
public long getBytePosition() {
return bytePosition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ private int getEncodedCharLength(int current) throws CharacterCodingException {
} else if (Character.isSurrogatePair(lChar, cChar)) {
return encoder.encode(
CharBuffer.wrap(new char[] {lChar, cChar})).limit();
} else throw new CharacterCodingException();
} else {
throw new CharacterCodingException();
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/test/java/org/apache/commons/csv/CSVParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -736,8 +736,7 @@ public void testGetRecordThreeBytesRead() throws Exception {
assertEquals(4, record.getRecordNumber());
assertEquals(code.indexOf('3'), record.getCharacterPosition());
assertEquals(record.getBytePosition(), 154);
};

}
}

@Test
Expand Down
34 changes: 18 additions & 16 deletions src/test/java/org/apache/commons/csv/JiraCsv196Test.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.csv;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -33,13 +35,13 @@ public void parseThreeBytes() throws IOException {
.setDelimiter(',')
.setQuote('\'')
.get();
CSVParser parser = new CSVParser.Builder()
final CSVParser parser = new CSVParser.Builder()
.setFormat(format)
.setReader(getTestInput("org/apache/commons/csv/CSV-196/japanese.csv"))
.setCharset(StandardCharsets.UTF_8)
.setEnableByteTracking(true)
.get();
long[] charByteKey = {0, 89, 242, 395};
final long[] charByteKey = {0, 89, 242, 395};
int idx = 0;
for (CSVRecord record : parser) {
assertEquals(charByteKey[idx++], record.getBytePosition());
Expand All @@ -54,13 +56,13 @@ public void parseFourBytes() throws IOException {
.setDelimiter(',')
.setQuote('\'')
.get();
CSVParser parser = new CSVParser.Builder()
final CSVParser parser = new CSVParser.Builder()
.setFormat(format)
.setReader(getTestInput("org/apache/commons/csv/CSV-196/emoji.csv"))
.setCharset(StandardCharsets.UTF_8)
.setEnableByteTracking(true)
.get();
long[] charByteKey = {0, 84, 701, 1318, 1935};
final long[] charByteKey = {0, 84, 701, 1318, 1935};
int idx = 0;
for (CSVRecord record : parser) {
assertEquals(charByteKey[idx++], record.getBytePosition());
Expand Down
Loading