comparison truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/source/Source.java @ 22036:f26b6524e5e0

fixes for empty sources
author Mick Jordan <mick.jordan@oracle.com>
date Thu, 23 Jul 2015 16:52:50 -0700
parents 72010b401152
children e7c2d36daf72
comparison
equal deleted inserted replaced
22035:100d2d3250d5 22036:f26b6524e5e0
1271 * 1271 *
1272 * @throws IllegalArgumentException if the offset is outside the string. 1272 * @throws IllegalArgumentException if the offset is outside the string.
1273 */ 1273 */
1274 public int offsetToLine(int offset) throws IllegalArgumentException { 1274 public int offsetToLine(int offset) throws IllegalArgumentException {
1275 if (offset < 0 || offset >= textLength) { 1275 if (offset < 0 || offset >= textLength) {
1276 if (offset == 0 && textLength == 0) {
1277 return 1;
1278 }
1276 throw new IllegalArgumentException("offset out of bounds"); 1279 throw new IllegalArgumentException("offset out of bounds");
1277 } 1280 }
1278 int line = 1; 1281 int line = 1;
1279 while (offset >= nlOffsets[line]) { 1282 while (offset >= nlOffsets[line]) {
1280 line++; 1283 line++;
1317 * would be the offset of a newline if the line is empty. 1320 * would be the offset of a newline if the line is empty.
1318 * 1321 *
1319 * @throws IllegalArgumentException if there is no such line in the text. 1322 * @throws IllegalArgumentException if there is no such line in the text.
1320 */ 1323 */
1321 public int lineStartOffset(int line) throws IllegalArgumentException { 1324 public int lineStartOffset(int line) throws IllegalArgumentException {
1322 if (textLength == 0 || lineOutOfRange(line)) { 1325 if (textLength == 0) {
1326 return 0;
1327 }
1328 if (lineOutOfRange(line)) {
1323 throw new IllegalArgumentException("line out of bounds"); 1329 throw new IllegalArgumentException("line out of bounds");
1324 } 1330 }
1325 return nlOffsets[line - 1]; 1331 return nlOffsets[line - 1];
1326 } 1332 }
1327 1333
1330 * <em>does not</em> include the final newline, if any. 1336 * <em>does not</em> include the final newline, if any.
1331 * 1337 *
1332 * @throws IllegalArgumentException if there is no such line in the text. 1338 * @throws IllegalArgumentException if there is no such line in the text.
1333 */ 1339 */
1334 public int lineLength(int line) throws IllegalArgumentException { 1340 public int lineLength(int line) throws IllegalArgumentException {
1335 if (textLength == 0 || lineOutOfRange(line)) { 1341 if (textLength == 0) {
1342 return 0;
1343 }
1344 if (lineOutOfRange(line)) {
1336 throw new IllegalArgumentException("line out of bounds"); 1345 throw new IllegalArgumentException("line out of bounds");
1337 } 1346 }
1338 if (line == nlOffsets.length - 1 && !finalNL) { 1347 if (line == nlOffsets.length - 1 && !finalNL) {
1339 return textLength - nlOffsets[line - 1]; 1348 return textLength - nlOffsets[line - 1];
1340 } 1349 }