Skip to content

Commit

Permalink
Fix compilation error on Nim 1.x, closes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
ba0f3 committed Sep 17, 2024
1 parent ad3c018 commit b66e794
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 9 deletions.
95 changes: 93 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,97 @@
# xxhash.nim
xxhash wrapper for Nim

**xxhash wrapper for Nim**
This is a wrapper for the `xxhash` hashing library in Nim, which provides fast, non-cryptographic hashing for various use cases such as file integrity checks, data storage, and more.

## Features
- Supports `XXH32`, `XXH64`, and `XXH128` hashing algorithms.
- Designed to work seamlessly with the Nim language.
- Efficient and fast hashing suitable for performance-sensitive applications.

## Installation
To use `xxhash.nim`, install the library using Nimble:

```shell
nimble install xxhash
```

Alternatively, you can clone the repository from the official source and add it to your project manually.

## Basic Usage
Below is a simple example of how to use the `xxhash` library in a Nim project.

### Importing the Module
```nim
import xxhash
```

### Hashing a String
To compute a 32-bit hash of a string using `XXH32`:

```nim
let hash32 = XXH32("Hello, world!")
echo hash32
```

To compute a 64-bit hash using `XXH64`:

```nim
let hash64 = XXH64("Hello, world!")
echo hash64
```

For larger data or more security, use `XXH128`:

```nim
let hash128 = XXH3_128bits("Hello, world!")
echo hash128
```

### Hashing a File
To hash a file, you can use:

```nim
import os
let fileContent = readFile("path/to/file")
let fileHash = XXH64(fileContent)
echo fileHash
```

### Streaming Hash Computation
For large files or streams, the xxhash library provides stateful hashing that allows hashing in chunks. Here's an example using `XXH64`:

```nim
var state = newXxh64()
# Process in chunks
state.update("Hello, ")
state.update("world!")
let finalHash = state.digest()
echo finalHash
# Important: Free the state after use in Nim 1.x.x
state.free()
```

## Note for Nim version 1.x.x
In Nim versions older than 2.0.0, you should manually call `state.free()` to deallocate resources because Nim does not automatically call destructors (`=destroy`). This step is crucial to avoid memory leaks when using the stateful API.

## Performance Considerations
- `xxhash` is designed to be very fast and is optimized for bulk data processing.
- `XXH128` is slightly slower but provides more robust hashing for larger datasets.

## Additional Information
This wrapper supports both 32-bit and 64-bit systems, ensuring compatibility across various platforms.

## Credits
This module uses [@rockcavera](https://github.com/rockcavera)'s [nint128](https://github.com/rockcavera/nim-nint128) for XXH128 computation
- This module utilizes [@rockcavera](https://github.com/rockcavera)'s [`nint128`](https://github.com/rockcavera/nim-nint128) for XXH128 computation.

## License
This project is distributed under the MIT License.


---

*This README was generated by [ChatGPT](https://openai.com/chatgpt).*
30 changes: 24 additions & 6 deletions src/xxhash.nim
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,12 @@ proc `$`*(state: Xxh32State): string =
proc reset*(state: Xxh32State, seed = 0'u32) =
state.llstate.XXH32_reset(seed)

proc `=destroy`*(state: Xxh32State) =
proc free*(state: Xxh32State) =
state.llstate.XXH32_freeState()

when (NimMajor, NimMinor, NimPatch) >= (2, 0, 0):
proc `=destroy`*(state: Xxh32State) =
state.free()

proc XXH3_createState*(): LLxxh3_64State {.cdecl, importc: "XXH3_createState".}
proc XXH3_freeState*(state: LLxxh3_64State) {.cdecl, importc: "XXH3_freeState".}
Expand All @@ -104,8 +107,12 @@ proc `$`*(state: XxH3_64State): string =
proc reset*(state: XxH3_64State, seed = 0'u64) =
state.llstate.XXH3_64_reset_withSeed(seed)

proc `=destroy`*(state: XxH3_64State) =
state.llstate.XXH3_freeState()
proc free*(state: XxH3_64State) =
state.llstate.XXH3_freeState()

when (NimMajor, NimMinor, NimPatch) >= (2, 0, 0):
proc `=destroy`*(state: XxH3_64State) =
state.free()

proc XXH64_createState*(): LLxxh64State {.cdecl, importc: "XXH64_createState".}
proc XXH64_freeState*(state: LLxxh64State) {.cdecl, importc: "XXH64_freeState".}
Expand All @@ -129,9 +136,13 @@ proc `$`*(state: Xxh64State): string =
proc reset*(state: Xxh64State, seed = 0'u64) =
state.llstate.XXH64_reset(seed)

proc `=destroy`*(state: Xxh64State) =
proc free*(state: Xxh64State) =
state.llstate.XXH64_freeState()

when (NimMajor, NimMinor, NimPatch) >= (2, 0, 0):
proc `=destroy`*(state: Xxh64State) =
state.free()

proc XXH128_reset*(state: LLxxh3_64State, seed: uint64 = 0) {.cdecl, importc: "XXH3_128bits_reset".}
proc XXH128_reset_withSeed*(state: LLxxh3_64State, seed: uint64 = 0) {.cdecl, importc: "XXH128_reset_withSeed".}
proc XXH128_reset_withSecret*(state: LLxxh3_64State, secret: cstring, len: int) {.cdecl, importc: "XXH128_reset_withSecret".}
Expand Down Expand Up @@ -161,11 +172,14 @@ proc reset*(state: Xxh128State, seed = 0'u64) =
proc resetWithSecret*(state: Xxh128State, secret: string) =
state.llstate.XXH128_reset_withSecret(secret.cstring, secret.len)

proc `=destroy`*(state: Xxh128State) =
proc free*(state: Xxh128State) =
state.llstate.XXH3_freeState()

when (NimMajor, NimMinor, NimPatch) >= (2, 0, 0):
proc `=destroy`*(state: Xxh128State) =
state.free()

when isMainModule:
import strutils
block:
# One Shot
assert 3794352943'u32 == XXH32("Nobody inspects the spammish repetition")
Expand Down Expand Up @@ -218,6 +232,8 @@ when isMainModule:
assert state.digest() == 3794352943'u32
assert $state == $3794352943'u32
state.reset()
when (NimMajor, NimMinor, NimPatch) < (2, 0, 0):
state.free()

block:
# HighLevel Streaming 64bit
Expand All @@ -226,3 +242,5 @@ when isMainModule:
assert state.digest() == msgh64
assert $state == $msgh64
state.reset()
when (NimMajor, NimMinor, NimPatch) < (2, 0, 0):
state.free()
2 changes: 1 addition & 1 deletion xxhash.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[Package]
name = "xxhash"
version = "0.11.0"
version = "0.12.0"
author = "Huy Doan"
description = "xxhash wrapper for Nim"
license = "MIT"
Expand Down

0 comments on commit b66e794

Please sign in to comment.