Skip to content

Commit 825e6b5

Browse files
l46kokcopybara-github
authored andcommitted
Move computing offset location into CelSourceHelper for reuse
PiperOrigin-RevId: 647456403
1 parent d63a63b commit 825e6b5

5 files changed

Lines changed: 46 additions & 37 deletions

File tree

common/src/main/java/dev/cel/common/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ java_library(
203203
name = "source",
204204
srcs = SOURCE_SOURCES,
205205
deps = [
206+
":source_location",
206207
"//common/annotations",
207208
"//common/internal",
208209
"@maven//:com_google_guava_guava",

common/src/main/java/dev/cel/common/CelSource.java

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public Optional<Integer> getLocationOffset(int line, int column) {
108108
* Get the line and column in the source expression text for the given code point {@code offset}.
109109
*/
110110
public Optional<CelSourceLocation> getOffsetLocation(int offset) {
111-
return getOffsetLocationImpl(lineOffsets, offset);
111+
return CelSourceHelper.getOffsetLocation(codePoints, offset);
112112
}
113113

114114
@Override
@@ -134,30 +134,6 @@ private static Optional<Integer> getLocationOffsetImpl(
134134
return Optional.of(offset + column);
135135
}
136136

137-
/**
138-
* Get the line and column in the source expression text for the given code point {@code offset}.
139-
*/
140-
public static Optional<CelSourceLocation> getOffsetLocationImpl(
141-
List<Integer> lineOffsets, int offset) {
142-
checkArgument(offset >= 0);
143-
LineAndOffset lineAndOffset = findLine(lineOffsets, offset);
144-
return Optional.of(CelSourceLocation.of(lineAndOffset.line, offset - lineAndOffset.offset));
145-
}
146-
147-
private static LineAndOffset findLine(List<Integer> lineOffsets, int offset) {
148-
int line = 1;
149-
for (int index = 0; index < lineOffsets.size(); index++) {
150-
if (lineOffsets.get(index) > offset) {
151-
break;
152-
}
153-
line++;
154-
}
155-
if (line == 1) {
156-
return new LineAndOffset(line, 0);
157-
}
158-
return new LineAndOffset(line, lineOffsets.get(line - 2));
159-
}
160-
161137
public Builder toBuilder() {
162138
return new Builder(codePoints, lineOffsets)
163139
.setDescription(description)
@@ -311,7 +287,7 @@ public Optional<Integer> getLocationOffset(int line, int column) {
311287
* offset}.
312288
*/
313289
public Optional<CelSourceLocation> getOffsetLocation(int offset) {
314-
return getOffsetLocationImpl(lineOffsets, offset);
290+
return CelSourceHelper.getOffsetLocation(codePoints, offset);
315291
}
316292

317293
@CheckReturnValue
@@ -335,17 +311,6 @@ public CelSource build() {
335311
}
336312
}
337313

338-
private static final class LineAndOffset {
339-
340-
private LineAndOffset(int line, int offset) {
341-
this.line = line;
342-
this.offset = offset;
343-
}
344-
345-
private final int line;
346-
private final int offset;
347-
}
348-
349314
/**
350315
* Tag for an extension that were used while parsing or type checking the source expression. For
351316
* example, optimizations that require special runtime support may be specified. These are used to

common/src/main/java/dev/cel/common/CelSourceHelper.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,40 @@ public static Optional<String> getSnippet(CelCodePointArray content, int line) {
4747
return Optional.of(end != start ? content.slice(start, end).toString() : "");
4848
}
4949

50+
/**
51+
* Get the line and column in the source expression text for the given code point {@code offset}.
52+
*/
53+
public static Optional<CelSourceLocation> getOffsetLocation(
54+
CelCodePointArray content, int offset) {
55+
checkArgument(offset >= 0);
56+
LineAndOffset lineAndOffset = findLine(content.lineOffsets(), offset);
57+
return Optional.of(CelSourceLocation.of(lineAndOffset.line, offset - lineAndOffset.offset));
58+
}
59+
60+
private static LineAndOffset findLine(List<Integer> lineOffsets, int offset) {
61+
int line = 1;
62+
for (Integer lineOffset : lineOffsets) {
63+
if (lineOffset > offset) {
64+
break;
65+
}
66+
line++;
67+
}
68+
if (line == 1) {
69+
return new LineAndOffset(line, 0);
70+
}
71+
return new LineAndOffset(line, lineOffsets.get(line - 2));
72+
}
73+
74+
private static final class LineAndOffset {
75+
private LineAndOffset(int line, int offset) {
76+
this.line = line;
77+
this.offset = offset;
78+
}
79+
80+
private final int line;
81+
private final int offset;
82+
}
83+
5084
static int findLineOffset(List<Integer> lineOffsets, int line) {
5185
if (line == 1) {
5286
return 0;

policy/src/main/java/dev/cel/policy/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ java_library(
3030
deps = [
3131
"//:auto_value",
3232
"//common:source",
33+
"//common:source_location",
3334
"//common/internal",
3435
"@maven//:com_google_errorprone_error_prone_annotations",
3536
"@maven//:com_google_guava_guava",

policy/src/main/java/dev/cel/policy/CelPolicySource.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.google.common.collect.ImmutableMap;
1919
import com.google.errorprone.annotations.CheckReturnValue;
2020
import dev.cel.common.CelSourceHelper;
21+
import dev.cel.common.CelSourceLocation;
2122
import dev.cel.common.Source;
2223
import dev.cel.common.internal.CelCodePointArray;
2324
import java.util.Map;
@@ -41,6 +42,13 @@ public Optional<String> getSnippet(int line) {
4142
return CelSourceHelper.getSnippet(getContent(), line);
4243
}
4344

45+
/**
46+
* Get the line and column in the source expression text for the given code point {@code offset}.
47+
*/
48+
public Optional<CelSourceLocation> getOffsetLocation(int offset) {
49+
return CelSourceHelper.getOffsetLocation(getContent(), offset);
50+
}
51+
4452
/** Builder for {@link CelPolicySource}. */
4553
@AutoValue.Builder
4654
public abstract static class Builder {

0 commit comments

Comments
 (0)