git.iko.soy/opencode-android
on master help sign in
40 lines 1.1 kB history raw
1// Copyright (c) Facebook, Inc. and its affiliates
2// SPDX-License-Identifier: MIT OR Apache-2.0
4package com.novi.serde;
6public final class Slice {
7 public final int start;
8 public final int end;
10 public Slice(int start, int end) {
11 this.start = start;
12 this.end = end;
13 }
15 // Lexicographic comparison between the (unsigned!) bytes referenced by `slice1` and `slice2`
16 // into `content`.
17 public static int compare_bytes(byte[] content, Slice slice1, Slice slice2) {
18 int start1 = slice1.start;
19 int end1 = slice1.end;
20 int start2 = slice2.start;
21 int end2 = slice2.end;
22 for (int i = 0; i < end1 - start1; i++) {
23 int byte1 = content[start1 + i] & 0xFF;
24 if (start2 + i >= end2) {
25 return 1;
26 }
27 int byte2 = content[start2 + i] & 0xFF;
28 if (byte1 > byte2) {
29 return 1;
30 }
31 if (byte1 < byte2) {
32 return -1;
33 }
34 }
35 if (end2 - start2 > end1 - start1) {
36 return -1;
37 }
38 return 0;
39 }