Skip to content

Commit

Permalink
(fix) lib: get compiled-widths and check for UTF-8
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-pelykh committed Jun 23, 2024
1 parent 8af1e40 commit 4ae7708
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 7 deletions.
75 changes: 75 additions & 0 deletions lib/src/main/java/org/pcre4j/Pcre2UtfWidth.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (C) 2024 Oleksii PELYKH
*
* This file is a part of the PCRE4J. The PCRE4J is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/
package org.pcre4j;

import org.pcre4j.api.IPcre2;

import java.util.Arrays;
import java.util.Optional;

/**
* The UTF character width.
*/
public enum Pcre2UtfWidth {
/**
* UTF-8
*/
UTF8(0b1),

/**
* UTF-16
*/
UTF16(0b10),

/**
* UTF-32
*/
UTF32(0b1000);

/**
* The integer value
*/
private final int value;

/**
* Create an enum entry with the given integer value.
*
* @param value the integer value
*/
private Pcre2UtfWidth(int value) {
this.value = value;
}

/**
* Get the enum entry by its integer value.
*
* @param value the integer value
* @return the enum entry
*/
public static Optional<Pcre2UtfWidth> valueOf(int value) {
return Arrays.stream(values())
.filter(entry -> entry.value == value)
.findFirst();
}

/**
* Get the integer value.
*
* @return the integer value
*/
public int value() {
return value;
}
}
7 changes: 6 additions & 1 deletion lib/src/main/java/org/pcre4j/Pcre4j.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private Pcre4j() {
}

/**
* Setup the Pcre4j.
* Set up the PCRE4J library.
*
* @param api the API to use
*/
Expand All @@ -35,6 +35,11 @@ public static void setup(IPcre2 api) {
throw new IllegalArgumentException("api must not be null");
}

final var compiledWidths = Pcre4jUtils.getCompiledWidths(api);
if (!compiledWidths.contains(Pcre2UtfWidth.UTF8)) {
throw new IllegalArgumentException("api must support UTF-8, yet it only supports " + compiledWidths);
}

synchronized (lock) {
Pcre4j.api = api;
}
Expand Down
22 changes: 17 additions & 5 deletions lib/src/main/java/org/pcre4j/Pcre4jUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -288,23 +289,34 @@ public static int getDefaultDepthLimit(IPcre2 api) {
}

/**
* Get which of the character width is compiled.
* Get which of the character widths the PCRE2 library was compiled with.
*
* @param api the PCRE2 API
* @return the compiled character width (8, 16, or 32)
*/
public static int getCompiledWidth(IPcre2 api) {
public static EnumSet<Pcre2UtfWidth> getCompiledWidths(IPcre2 api) {
if (api == null) {
throw new IllegalArgumentException("api must not be null");
}

final var width = new int[1];
final var result = api.config(IPcre2.CONFIG_COMPILED_WIDTHS, width);
final var widthsMask = new int[1];
final var result = api.config(IPcre2.CONFIG_COMPILED_WIDTHS, widthsMask);
if (result < 0) {
throw new IllegalStateException(getErrorMessage(api, result));
}

return width[0];
final var widths = EnumSet.noneOf(Pcre2UtfWidth.class);
if ((widthsMask[0] & Pcre2UtfWidth.UTF8.value()) != 0) {
widths.add(Pcre2UtfWidth.UTF8);
}
if ((widthsMask[0] & Pcre2UtfWidth.UTF16.value()) != 0) {
widths.add(Pcre2UtfWidth.UTF16);
}
if ((widthsMask[0] & Pcre2UtfWidth.UTF32.value()) != 0) {
widths.add(Pcre2UtfWidth.UTF32);
}

return widths;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/src/main/java/org/pcre4j/test/Pcre2Tests.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void config() {
Pcre4jUtils.isJitSupported(api);
Pcre4jUtils.getDefaultHeapLimit(api);
Pcre4jUtils.getDefaultDepthLimit(api);
Pcre4jUtils.getCompiledWidth(api);
Pcre4jUtils.getCompiledWidths(api);
Pcre4jUtils.getDefaultBsr(api);
}

Expand Down

0 comments on commit 4ae7708

Please sign in to comment.