Skip to content

Commit

Permalink
Added Unmarshal::unmarshalStringPointer
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Mar 9, 2024
1 parent f0b6243 commit 30c07c7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Import as a Gradle dependency:

```groovy
dependencies {
implementation("io.github.over-run:marshal:0.1.0-alpha.22-jdk22")
implementation("io.github.over-run:marshal:0.1.0-alpha.23-jdk22")
}
```

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ projGroupId=io.github.over-run
projArtifactId=marshal
# The project name should only contain lowercase letters, numbers and hyphen.
projName=marshal
projVersion=0.1.0-alpha.22-jdk22
projVersion=0.1.0-alpha.23-jdk22
projDesc=Marshaler of native libraries
# Uncomment them if you want to publish to maven repository.
projUrl=https://github.com/Over-Run/marshal
Expand Down
37 changes: 29 additions & 8 deletions src/main/java/overrun/marshal/Unmarshal.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.lang.foreign.MemorySegment;
import java.lang.invoke.VarHandle;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.function.Function;
import java.util.function.IntFunction;

Expand Down Expand Up @@ -136,7 +137,7 @@ public static boolean unmarshalAsBoolean(double v) {
* @return the string
*/
public static @Nullable String unmarshalAsString(MemorySegment segment) {
return isNullPointer(segment) ? null : segment.getString(0L);
return unmarshalAsString(segment, StandardCharsets.UTF_8);
}

/**
Expand All @@ -157,7 +158,7 @@ public static boolean unmarshalAsBoolean(double v) {
* @return the string
*/
public static @Nullable String unboundString(MemorySegment segment) {
return isNullPointer(segment) ? null : segment.reinterpret(STR_SIZE).getString(0L);
return unboundString(segment, StandardCharsets.UTF_8);
}

/**
Expand All @@ -171,6 +172,29 @@ public static boolean unmarshalAsBoolean(double v) {
return isNullPointer(segment) ? null : segment.reinterpret(STR_SIZE).getString(0L, charset);
}

/**
* Unmarshal the given segment as a string.
*
* @param segment the segment which is pointer to a string
* @return the string
*/
public static @Nullable String unmarshalStringPointer(MemorySegment segment) {
return unmarshalStringPointer(segment, StandardCharsets.UTF_8);
}

/**
* Unmarshal the given segment as a string.
*
* @param segment the segment which is pointer to a string
* @param charset the charset
* @return the string
*/
public static @Nullable String unmarshalStringPointer(MemorySegment segment, Charset charset) {
if (isNullPointer(segment)) return null;
final MemorySegment pointer = segment.get(STR_LAYOUT, 0L);
return isNullPointer(pointer) ? null : pointer.getString(0L, charset);
}

/**
* Unmarshal the given segment as an array.
*
Expand Down Expand Up @@ -388,7 +412,7 @@ public static boolean unmarshalAsBoolean(double v) {
* @return the array
*/
public static String @Nullable [] unmarshalAsStringArray(AddressLayout elementLayout, MemorySegment segment) {
return unmarshal(elementLayout, segment, String[]::new, s -> s.get(STR_LAYOUT, 0L).getString(0L));
return unmarshalAsStringArray(elementLayout, segment, StandardCharsets.UTF_8);
}

/**
Expand All @@ -410,7 +434,7 @@ public static boolean unmarshalAsBoolean(double v) {
* @return the array
*/
public static String @Nullable [] unmarshalAsStringArray(MemorySegment segment) {
return unmarshalAsStringArray(ADDRESS, segment);
return unmarshalAsStringArray(segment, StandardCharsets.UTF_8);
}

/**
Expand Down Expand Up @@ -532,10 +556,7 @@ public static void copy(MemorySegment src, double @Nullable [] dst) {
* @param dst the destination
*/
public static void copy(MemorySegment src, String @Nullable [] dst) {
if (isNullPointer(src) || dst == null) return;
for (int i = 0; i < dst.length; i++) {
dst[i] = ((MemorySegment) vh_stringArray.get(src, (long) i)).getString(0L);
}
copy(src, dst, StandardCharsets.UTF_8);
}

/**
Expand Down

0 comments on commit 30c07c7

Please sign in to comment.