-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(java): expose hash function, use OptionalLong in TextChange
- Loading branch information
Showing
5 changed files
with
42 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package mp.code; | ||
|
||
public class Utils { | ||
/** | ||
* Hashes the given {@link String} using CodeMP's hashing algorithm (xxh3). | ||
* @param input the string to hash | ||
* @return the hash | ||
*/ | ||
public static native long hash(String input); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,17 @@ | ||
package mp.code.data; | ||
|
||
import java.util.OptionalLong; | ||
|
||
public class TextChange { | ||
public final long start; | ||
public final long end; | ||
public final String content; | ||
private final long hash; // xxh3 hash | ||
public final OptionalLong hash; // xxh3 hash | ||
|
||
public TextChange(long start, long end, String content, long hash) { | ||
public TextChange(long start, long end, String content, OptionalLong hash) { | ||
this.start = start; | ||
this.end = end; | ||
this.content = content; | ||
this.hash = hash; | ||
} | ||
|
||
private static native long hash(String content); | ||
public boolean hashMatches(String content) { | ||
// 0 is Rust default value and a very unlikely hash | ||
return hash == 0L || this.hash == hash(content); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use jni::{objects::{JClass, JString}, sys::jlong, JNIEnv}; | ||
|
||
use super::JExceptable; | ||
|
||
/// Calculates the XXH3 hash for a given String. | ||
#[no_mangle] | ||
pub extern "system" fn Java_mp_code_Utils_hash<'local>( | ||
mut env: JNIEnv, | ||
_class: JClass<'local>, | ||
content: JString<'local>, | ||
) -> jlong { | ||
let content: String = env.get_string(&content) | ||
.map(|s| s.into()) | ||
.jexcept(&mut env); | ||
let hash = crate::ext::hash(content.as_bytes()); | ||
i64::from_ne_bytes(hash.to_ne_bytes()) | ||
} |