@javascriptcommon/react-native-xxhash 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -0
- package/README.md +104 -0
- package/android/CMakeLists.txt +48 -0
- package/android/build.gradle +154 -0
- package/android/cpp-adapter.cpp +38 -0
- package/android/gradle.properties +5 -0
- package/android/src/main/AndroidManifest.xml +3 -0
- package/android/src/main/AndroidManifestNew.xml +2 -0
- package/android/src/main/java/com/xxhash/XxhashModule.kt +55 -0
- package/android/src/main/java/com/xxhash/XxhashPackage.kt +17 -0
- package/cpp/react-native-xxhash.cpp +43 -0
- package/cpp/react-native-xxhash.h +27 -0
- package/cpp/xxhash.c +42 -0
- package/cpp/xxhash.h +7238 -0
- package/ios/Xxhash.h +9 -0
- package/ios/Xxhash.mm +62 -0
- package/lib/commonjs/index.js +78 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/module/index.js +71 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/typescript/index.d.ts +41 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/package.json +192 -0
- package/react-native-xxhash.podspec +41 -0
- package/src/index.tsx +82 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 pioner921227
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
in the Software without restriction, including without limitation the rights
|
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# react-native-xxhash
|
|
2
|
+
|
|
3
|
+
A React Native library for hashing strings using the fast and deterministic xxHash algorithm, written in C++ with JSI for high performance. This library provides support for both 64-bit and 128-bit hashing.
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
## Features
|
|
7
|
+
|
|
8
|
+
- **High Performance**: xxHash is one of the fastest non-cryptographic hash functions.
|
|
9
|
+
- **Deterministic Hashing**: Ensures consistent results for the same input.
|
|
10
|
+
- **128-bit and 64-bit Support**: Choose between 128-bit and 64-bit hash outputs based on your use case.
|
|
11
|
+
- **Cross-Platform**: Supports both iOS and Android in React Native projects.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
To install the library, use either `npm` or `yarn`:
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
npm install react-native-xxhash
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
yarn add react-native-xxhash
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## iOS
|
|
26
|
+
```sh
|
|
27
|
+
pod install
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## Usage
|
|
33
|
+
|
|
34
|
+
Here’s how to use the `react-native-xxhash` library in your React Native project:
|
|
35
|
+
|
|
36
|
+
### Import the Functions
|
|
37
|
+
```javascript
|
|
38
|
+
import { hash128, hash64 } from 'react-native-xxhash';
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Hash a String (128-bit)
|
|
42
|
+
This function generates a fast and deterministic 128-bit hash for a given string input.
|
|
43
|
+
|
|
44
|
+
```javascript
|
|
45
|
+
const resultHash128 = hash128("hello world");
|
|
46
|
+
console.log('128-bit hash:', resultHash128);
|
|
47
|
+
// Output: A 128-bit hash string
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Hash a String (64-bit)
|
|
51
|
+
This function generates a fast and deterministic 64-bit hash for a given string input.
|
|
52
|
+
|
|
53
|
+
```javascript
|
|
54
|
+
const resultHash64 = hash64("hello world");
|
|
55
|
+
console.log('64-bit hash:', resultHash64);
|
|
56
|
+
// Output: A 64-bit hash string
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Example Usage in a Component
|
|
60
|
+
```javascript
|
|
61
|
+
import React, { useEffect } from 'react';
|
|
62
|
+
import { Text, View } from 'react-native';
|
|
63
|
+
import { hash128, hash64 } from 'react-native-xxhash';
|
|
64
|
+
|
|
65
|
+
const App = () => {
|
|
66
|
+
useEffect(() => {
|
|
67
|
+
const hash128Result = hash128("react-native");
|
|
68
|
+
const hash64Result = hash64("react-native");
|
|
69
|
+
|
|
70
|
+
console.log("128-bit hash:", hash128Result);
|
|
71
|
+
console.log("64-bit hash:", hash64Result);
|
|
72
|
+
}, []);
|
|
73
|
+
|
|
74
|
+
return (
|
|
75
|
+
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
|
|
76
|
+
<Text>Check your console for hash results!</Text>
|
|
77
|
+
</View>
|
|
78
|
+
);
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export default App;
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
## API Reference
|
|
87
|
+
|
|
88
|
+
### `hash128(input: string): string`
|
|
89
|
+
- **Description**: Generates a 128-bit hash for the given string input.
|
|
90
|
+
- **Parameters**:
|
|
91
|
+
- `input` (string): The string to hash.
|
|
92
|
+
- **Returns**: A 128-bit hash as a string.
|
|
93
|
+
|
|
94
|
+
### `hash64(input: string): string`
|
|
95
|
+
- **Description**: Generates a 64-bit hash for the given string input.
|
|
96
|
+
- **Parameters**:
|
|
97
|
+
- `input` (string): The string to hash.
|
|
98
|
+
- **Returns**: A 64-bit hash as a string.
|
|
99
|
+
|
|
100
|
+
---
|
|
101
|
+
|
|
102
|
+
## License
|
|
103
|
+
|
|
104
|
+
`react-native-xxhash` is released under the MIT License. See the [LICENSE](LICENSE) file for details.
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
cmake_minimum_required(VERSION 3.4.1)
|
|
2
|
+
project(react-native-xxhash)
|
|
3
|
+
|
|
4
|
+
set (CMAKE_VERBOSE_MAKEFILE ON)
|
|
5
|
+
set (CMAKE_CXX_STANDARD 17)
|
|
6
|
+
|
|
7
|
+
add_library(react-native-xxhash SHARED
|
|
8
|
+
../cpp/react-native-xxhash.cpp
|
|
9
|
+
../cpp/xxhash.c
|
|
10
|
+
cpp-adapter.cpp
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
find_package(ReactAndroid REQUIRED CONFIG)
|
|
14
|
+
|
|
15
|
+
find_package(fbjni REQUIRED CONFIG)
|
|
16
|
+
|
|
17
|
+
target_include_directories(
|
|
18
|
+
react-native-xxhash PRIVATE
|
|
19
|
+
"${NODE_MODULES_DIR}/react-native/React"
|
|
20
|
+
"${NODE_MODULES_DIR}/react-native/React/Base"
|
|
21
|
+
"${NODE_MODULES_DIR}/react-native/ReactCommon"
|
|
22
|
+
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi"
|
|
23
|
+
# "${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni/react/turbomodule"
|
|
24
|
+
# "${NODE_MODULES_DIR}/react-native/ReactCommon/runtimeexecutor"
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
# Specifies a path to native header files.
|
|
28
|
+
include_directories(
|
|
29
|
+
../cpp
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
if (ReactAndroid_VERSION_MINOR GREATER_EQUAL 76)
|
|
33
|
+
target_link_libraries(
|
|
34
|
+
react-native-xxhash
|
|
35
|
+
ReactAndroid::jsi
|
|
36
|
+
log
|
|
37
|
+
ReactAndroid::reactnative
|
|
38
|
+
fbjni::fbjni
|
|
39
|
+
)
|
|
40
|
+
else()
|
|
41
|
+
target_link_libraries(
|
|
42
|
+
react-native-xxhash
|
|
43
|
+
ReactAndroid::jsi
|
|
44
|
+
log
|
|
45
|
+
ReactAndroid::reactnativejni
|
|
46
|
+
fbjni::fbjni
|
|
47
|
+
)
|
|
48
|
+
endif()
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import groovy.json.JsonSlurper
|
|
2
|
+
import org.apache.tools.ant.filters.ReplaceTokens
|
|
3
|
+
import java.nio.file.Paths
|
|
4
|
+
|
|
5
|
+
buildscript {
|
|
6
|
+
// Buildscript is evaluated before everything else so we can't use getExtOrDefault
|
|
7
|
+
def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["Xxhash_kotlinVersion"]
|
|
8
|
+
|
|
9
|
+
repositories {
|
|
10
|
+
google()
|
|
11
|
+
mavenCentral()
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
dependencies {
|
|
15
|
+
classpath "com.android.tools.build:gradle:7.2.1"
|
|
16
|
+
// noinspection DifferentKotlinGradleVersion
|
|
17
|
+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
static def findNodeModules(baseDir) {
|
|
22
|
+
def basePath = baseDir.toPath().normalize()
|
|
23
|
+
// Node's module resolution algorithm searches up to the root directory,
|
|
24
|
+
// after which the base path will be null
|
|
25
|
+
while (basePath) {
|
|
26
|
+
def nodeModulesPath = Paths.get(basePath.toString(), "node_modules")
|
|
27
|
+
def reactNativePath = Paths.get(nodeModulesPath.toString(), "react-native")
|
|
28
|
+
if (nodeModulesPath.toFile().exists() && reactNativePath.toFile().exists()) {
|
|
29
|
+
return nodeModulesPath.toString()
|
|
30
|
+
}
|
|
31
|
+
basePath = basePath.getParent()
|
|
32
|
+
}
|
|
33
|
+
throw new GradleException("react-native-xxhash: Failed to find node_modules/ path!")
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
def nodeModules = findNodeModules(projectDir)
|
|
37
|
+
logger.warn("react-native-xxhash: node_modules/ found at: ${nodeModules}")
|
|
38
|
+
|
|
39
|
+
def reactNativeArchitectures() {
|
|
40
|
+
def value = rootProject.getProperties().get("reactNativeArchitectures")
|
|
41
|
+
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
def isNewArchitectureEnabled() {
|
|
45
|
+
return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
apply plugin: "com.android.library"
|
|
49
|
+
apply plugin: "kotlin-android"
|
|
50
|
+
|
|
51
|
+
if (isNewArchitectureEnabled()) {
|
|
52
|
+
apply plugin: "com.facebook.react"
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
def getExtOrDefault(name) {
|
|
56
|
+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["Xxhash_" + name]
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
def getExtOrIntegerDefault(name) {
|
|
60
|
+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["Xxhash_" + name]).toInteger()
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
def supportsNamespace() {
|
|
64
|
+
def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
|
|
65
|
+
def major = parsed[0].toInteger()
|
|
66
|
+
def minor = parsed[1].toInteger()
|
|
67
|
+
|
|
68
|
+
// Namespace support was added in 7.3.0
|
|
69
|
+
return (major == 7 && minor >= 3) || major >= 8
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
android {
|
|
73
|
+
if (supportsNamespace()) {
|
|
74
|
+
namespace "com.xxhash"
|
|
75
|
+
|
|
76
|
+
sourceSets {
|
|
77
|
+
main {
|
|
78
|
+
manifest.srcFile "src/main/AndroidManifestNew.xml"
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
ndkVersion getExtOrDefault("ndkVersion")
|
|
84
|
+
compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
|
|
85
|
+
|
|
86
|
+
defaultConfig {
|
|
87
|
+
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
|
|
88
|
+
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
|
|
89
|
+
|
|
90
|
+
externalNativeBuild {
|
|
91
|
+
cmake {
|
|
92
|
+
cppFlags "-O2 -frtti -fexceptions -Wall -fstack-protector-all"
|
|
93
|
+
abiFilters (*reactNativeArchitectures())
|
|
94
|
+
arguments "-DANDROID_STL=c++_shared",
|
|
95
|
+
"-DNODE_MODULES_DIR=${nodeModules}",
|
|
96
|
+
"-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
externalNativeBuild {
|
|
102
|
+
cmake {
|
|
103
|
+
path "CMakeLists.txt"
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
buildTypes {
|
|
108
|
+
release {
|
|
109
|
+
minifyEnabled false
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
buildFeatures {
|
|
114
|
+
buildConfig true
|
|
115
|
+
prefab true
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
packagingOptions {
|
|
119
|
+
excludes = [
|
|
120
|
+
"**/libjsi.so",
|
|
121
|
+
"**/libreactnativejni.so",
|
|
122
|
+
"**/libreact_nativemodule_core.so",
|
|
123
|
+
"**/libturbomodulejsijni.so",
|
|
124
|
+
"**/libc++_shared.so",
|
|
125
|
+
"**/libfbjni.so",
|
|
126
|
+
"**/libreactnative.so",
|
|
127
|
+
]
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
lintOptions {
|
|
131
|
+
disable "GradleCompatible"
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
compileOptions {
|
|
135
|
+
sourceCompatibility JavaVersion.VERSION_1_8
|
|
136
|
+
targetCompatibility JavaVersion.VERSION_1_8
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
repositories {
|
|
141
|
+
mavenCentral()
|
|
142
|
+
google()
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
def kotlin_version = getExtOrDefault("kotlinVersion")
|
|
146
|
+
|
|
147
|
+
dependencies {
|
|
148
|
+
// For < 0.71, this will be from the local maven repo
|
|
149
|
+
// For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
|
|
150
|
+
//noinspection GradleDynamicVersion
|
|
151
|
+
implementation "com.facebook.react:react-native:+"
|
|
152
|
+
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
|
153
|
+
}
|
|
154
|
+
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
#include <jni.h>
|
|
2
|
+
#include "react-native-xxhash.h"
|
|
3
|
+
#include "jsi/jsi.h"
|
|
4
|
+
#include <android/log.h>
|
|
5
|
+
#include <fbjni/fbjni.h>
|
|
6
|
+
#include <fbjni/detail/Registration.h>
|
|
7
|
+
#include <typeinfo>
|
|
8
|
+
|
|
9
|
+
//XXHashBridge
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
using namespace facebook;
|
|
13
|
+
|
|
14
|
+
struct XXHashBridge : jni::JavaClass<XXHashBridge> {
|
|
15
|
+
public:
|
|
16
|
+
static constexpr auto kJavaDescriptor = "Lcom/xxhash/XxhashModule;";
|
|
17
|
+
|
|
18
|
+
static void registerNatives() {
|
|
19
|
+
javaClassStatic()->registerNatives({
|
|
20
|
+
makeNativeMethod("nativeInstall", XXHashBridge::nativeInstall)
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
private:
|
|
24
|
+
static void nativeInstall(
|
|
25
|
+
jni::alias_ref<jni::JObject> thiz,
|
|
26
|
+
jlong jsiRuntimePointer
|
|
27
|
+
) {
|
|
28
|
+
|
|
29
|
+
// Реализация
|
|
30
|
+
auto jsiRuntime = reinterpret_cast<jsi::Runtime*>(jsiRuntimePointer);
|
|
31
|
+
// Установите JSI
|
|
32
|
+
xxhash::install(jsiRuntime);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *) {
|
|
37
|
+
return jni::initialize(vm, [] { XXHashBridge::registerNatives(); });
|
|
38
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
package com.xxhash
|
|
2
|
+
|
|
3
|
+
import android.util.Log
|
|
4
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
5
|
+
import com.facebook.react.bridge.ReactContextBaseJavaModule
|
|
6
|
+
import com.facebook.react.bridge.ReactMethod
|
|
7
|
+
import com.facebook.react.common.annotations.FrameworkAPI
|
|
8
|
+
|
|
9
|
+
class XxhashModule internal constructor(val context: ReactApplicationContext) :
|
|
10
|
+
ReactContextBaseJavaModule(context) {
|
|
11
|
+
|
|
12
|
+
companion object {
|
|
13
|
+
const val NAME = "xxhash"
|
|
14
|
+
|
|
15
|
+
init {
|
|
16
|
+
System.loadLibrary("react-native-xxhash")
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
@OptIn(FrameworkAPI::class)
|
|
20
|
+
@JvmStatic
|
|
21
|
+
external fun nativeInstall(jsiRuntimePointer: Long)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
private val reactContext = context
|
|
25
|
+
|
|
26
|
+
override fun getName(): String {
|
|
27
|
+
return NAME
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// New Architecture / bridgeless: install eagerly when the context is ready so
|
|
31
|
+
// __xxhash128/__xxhash64 are available before the first hash() call, and we
|
|
32
|
+
// never depend on JS calling install(). Guarded so a null/zero
|
|
33
|
+
// javaScriptContextHolder (possible under bridgeless) can never NPE.
|
|
34
|
+
override fun initialize() {
|
|
35
|
+
super.initialize()
|
|
36
|
+
tryInstall()
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
@OptIn(FrameworkAPI::class)
|
|
40
|
+
@ReactMethod(isBlockingSynchronousMethod = true)
|
|
41
|
+
fun install(): Boolean {
|
|
42
|
+
return tryInstall()
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
@OptIn(FrameworkAPI::class)
|
|
46
|
+
private fun tryInstall(): Boolean {
|
|
47
|
+
val pointer = reactContext.javaScriptContextHolder?.get() ?: 0L
|
|
48
|
+
if (pointer == 0L) {
|
|
49
|
+
Log.w("xxhash", "install skipped: no JSI runtime pointer (bridgeless?)")
|
|
50
|
+
return false
|
|
51
|
+
}
|
|
52
|
+
nativeInstall(pointer)
|
|
53
|
+
return true
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
package com.xxhash
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.ReactPackage
|
|
4
|
+
import com.facebook.react.bridge.NativeModule
|
|
5
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
6
|
+
import com.facebook.react.uimanager.ViewManager
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class XxhashPackage : ReactPackage {
|
|
10
|
+
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
|
|
11
|
+
return listOf(XxhashModule(reactContext))
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
|
|
15
|
+
return emptyList()
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
#include "react-native-xxhash.h"
|
|
2
|
+
|
|
3
|
+
void xxhash::install(jsi::Runtime* rt_ptr) {
|
|
4
|
+
jsi::Runtime& runtime = *rt_ptr;
|
|
5
|
+
|
|
6
|
+
jsi::Function hash128 = jsi::Function::createFromHostFunction(
|
|
7
|
+
runtime, jsi::PropNameID::forAscii(runtime, "hash128"), 1,
|
|
8
|
+
[](jsi::Runtime& rt, const jsi::Value& thisVal, const jsi::Value* args,
|
|
9
|
+
size_t count) {
|
|
10
|
+
const jsi::Value& arg = args[0];
|
|
11
|
+
|
|
12
|
+
if (!arg.isString()) [[unlikely]] {
|
|
13
|
+
throw jsi::JSError(rt, "Argument is not a 'string'");
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
char result[33];
|
|
17
|
+
|
|
18
|
+
xxhash::make_hash_128(arg.asString(rt).utf8(rt), result);
|
|
19
|
+
|
|
20
|
+
return jsi::String::createFromUtf8(rt, result);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
jsi::Function hash64 = jsi::Function::createFromHostFunction(
|
|
24
|
+
runtime, jsi::PropNameID::forAscii(runtime, "hash64"), 1,
|
|
25
|
+
[](jsi::Runtime& rt, const jsi::Value& thisVal, const jsi::Value* args,
|
|
26
|
+
size_t count) {
|
|
27
|
+
const jsi::Value& arg = args[0];
|
|
28
|
+
|
|
29
|
+
if (!arg.isString()) [[unlikely]] {
|
|
30
|
+
throw jsi::JSError(rt, "Argument is not a 'string'");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
char result[17];
|
|
34
|
+
|
|
35
|
+
xxhash::make_hash_64(arg.asString(rt).utf8(rt), result);
|
|
36
|
+
|
|
37
|
+
return jsi::String::createFromUtf8(rt, result);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
runtime.global().setProperty(runtime, "__xxhash128", std::move(hash128));
|
|
41
|
+
|
|
42
|
+
runtime.global().setProperty(runtime, "__xxhash64", std::move(hash64));
|
|
43
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
#ifndef XXHASH_H
|
|
3
|
+
#define XXHASH_H
|
|
4
|
+
#include <jsi/jsi.h>
|
|
5
|
+
#include <iomanip>
|
|
6
|
+
#include <sstream>
|
|
7
|
+
#include "xxhash.h"
|
|
8
|
+
|
|
9
|
+
using namespace facebook;
|
|
10
|
+
|
|
11
|
+
namespace xxhash {
|
|
12
|
+
|
|
13
|
+
inline void make_hash_64(const std::string_view str, char result[17]) noexcept {
|
|
14
|
+
XXH64_hash_t hash = XXH3_64bits(str.data(), str.size());
|
|
15
|
+
std::snprintf(result, 17, "%016llx", hash);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
inline void make_hash_128(const std::string_view str,
|
|
19
|
+
char result[33]) noexcept {
|
|
20
|
+
XXH128_hash_t hash = XXH3_128bits(str.data(), str.size());
|
|
21
|
+
std::snprintf(result, 33, "%016llx%016llx", hash.high64, hash.low64);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
void install(jsi::Runtime* rt);
|
|
25
|
+
} // namespace xxhash
|
|
26
|
+
|
|
27
|
+
#endif /* XXHASH_H */
|
package/cpp/xxhash.c
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xxHash - Extremely Fast Hash algorithm
|
|
3
|
+
* Copyright (C) 2012-2023 Yann Collet
|
|
4
|
+
*
|
|
5
|
+
* BSD 2-Clause License (https://www.opensource.org/licenses/bsd-license.php)
|
|
6
|
+
*
|
|
7
|
+
* Redistribution and use in source and binary forms, with or without
|
|
8
|
+
* modification, are permitted provided that the following conditions are
|
|
9
|
+
* met:
|
|
10
|
+
*
|
|
11
|
+
* * Redistributions of source code must retain the above copyright
|
|
12
|
+
* notice, this list of conditions and the following disclaimer.
|
|
13
|
+
* * Redistributions in binary form must reproduce the above
|
|
14
|
+
* copyright notice, this list of conditions and the following disclaimer
|
|
15
|
+
* in the documentation and/or other materials provided with the
|
|
16
|
+
* distribution.
|
|
17
|
+
*
|
|
18
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
19
|
+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
20
|
+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
21
|
+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
22
|
+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
23
|
+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
24
|
+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
25
|
+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
26
|
+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
27
|
+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
29
|
+
*
|
|
30
|
+
* You can contact the author at:
|
|
31
|
+
* - xxHash homepage: https://www.xxhash.com
|
|
32
|
+
* - xxHash source repository: https://github.com/Cyan4973/xxHash
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
/*
|
|
36
|
+
* xxhash.c instantiates functions defined in xxhash.h
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
#define XXH_STATIC_LINKING_ONLY /* access advanced declarations */
|
|
40
|
+
#define XXH_IMPLEMENTATION /* access definitions */
|
|
41
|
+
|
|
42
|
+
#include "xxhash.h"
|