Skip to content

Commit

Permalink
Move computing offset location into CelSourceHelper for reuse
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 648869686
  • Loading branch information
l46kok authored and copybara-github committed Jul 2, 2024
1 parent d63a63b commit 3d2aada
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 37 deletions.
1 change: 1 addition & 0 deletions common/src/main/java/dev/cel/common/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ java_library(
name = "source",
srcs = SOURCE_SOURCES,
deps = [
":source_location",
"//common/annotations",
"//common/internal",
"@maven//:com_google_guava_guava",
Expand Down
39 changes: 2 additions & 37 deletions common/src/main/java/dev/cel/common/CelSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public Optional<Integer> getLocationOffset(int line, int column) {
* Get the line and column in the source expression text for the given code point {@code offset}.
*/
public Optional<CelSourceLocation> getOffsetLocation(int offset) {
return getOffsetLocationImpl(lineOffsets, offset);
return CelSourceHelper.getOffsetLocation(codePoints, offset);
}

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

/**
* Get the line and column in the source expression text for the given code point {@code offset}.
*/
public static Optional<CelSourceLocation> getOffsetLocationImpl(
List<Integer> lineOffsets, int offset) {
checkArgument(offset >= 0);
LineAndOffset lineAndOffset = findLine(lineOffsets, offset);
return Optional.of(CelSourceLocation.of(lineAndOffset.line, offset - lineAndOffset.offset));
}

private static LineAndOffset findLine(List<Integer> lineOffsets, int offset) {
int line = 1;
for (int index = 0; index < lineOffsets.size(); index++) {
if (lineOffsets.get(index) > offset) {
break;
}
line++;
}
if (line == 1) {
return new LineAndOffset(line, 0);
}
return new LineAndOffset(line, lineOffsets.get(line - 2));
}

public Builder toBuilder() {
return new Builder(codePoints, lineOffsets)
.setDescription(description)
Expand Down Expand Up @@ -311,7 +287,7 @@ public Optional<Integer> getLocationOffset(int line, int column) {
* offset}.
*/
public Optional<CelSourceLocation> getOffsetLocation(int offset) {
return getOffsetLocationImpl(lineOffsets, offset);
return CelSourceHelper.getOffsetLocation(codePoints, offset);
}

@CheckReturnValue
Expand All @@ -335,17 +311,6 @@ public CelSource build() {
}
}

private static final class LineAndOffset {

private LineAndOffset(int line, int offset) {
this.line = line;
this.offset = offset;
}

private final int line;
private final int offset;
}

/**
* Tag for an extension that were used while parsing or type checking the source expression. For
* example, optimizations that require special runtime support may be specified. These are used to
Expand Down
34 changes: 34 additions & 0 deletions common/src/main/java/dev/cel/common/CelSourceHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,40 @@ public static Optional<String> getSnippet(CelCodePointArray content, int line) {
return Optional.of(end != start ? content.slice(start, end).toString() : "");
}

/**
* Get the line and column in the source expression text for the given code point {@code offset}.
*/
public static Optional<CelSourceLocation> getOffsetLocation(
CelCodePointArray content, int offset) {
checkArgument(offset >= 0);
LineAndOffset lineAndOffset = findLine(content.lineOffsets(), offset);
return Optional.of(CelSourceLocation.of(lineAndOffset.line, offset - lineAndOffset.offset));
}

private static LineAndOffset findLine(List<Integer> lineOffsets, int offset) {
int line = 1;
for (Integer lineOffset : lineOffsets) {
if (lineOffset > offset) {
break;
}
line++;
}
if (line == 1) {
return new LineAndOffset(line, 0);
}
return new LineAndOffset(line, lineOffsets.get(line - 2));
}

private static final class LineAndOffset {
private LineAndOffset(int line, int offset) {
this.line = line;
this.offset = offset;
}

private final int line;
private final int offset;
}

static int findLineOffset(List<Integer> lineOffsets, int line) {
if (line == 1) {
return 0;
Expand Down
1 change: 1 addition & 0 deletions policy/src/main/java/dev/cel/policy/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ java_library(
deps = [
"//:auto_value",
"//common:source",
"//common:source_location",
"//common/internal",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
Expand Down
8 changes: 8 additions & 0 deletions policy/src/main/java/dev/cel/policy/CelPolicySource.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.errorprone.annotations.CheckReturnValue;
import dev.cel.common.CelSourceHelper;
import dev.cel.common.CelSourceLocation;
import dev.cel.common.Source;
import dev.cel.common.internal.CelCodePointArray;
import java.util.Map;
Expand All @@ -41,6 +42,13 @@ public Optional<String> getSnippet(int line) {
return CelSourceHelper.getSnippet(getContent(), line);
}

/**
* Get the line and column in the source expression text for the given code point {@code offset}.
*/
public Optional<CelSourceLocation> getOffsetLocation(int offset) {
return CelSourceHelper.getOffsetLocation(getContent(), offset);
}

/** Builder for {@link CelPolicySource}. */
@AutoValue.Builder
public abstract static class Builder {
Expand Down

0 comments on commit 3d2aada

Please sign in to comment.