-
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.
Merge pull request #2 from bosondata/feature/allow-nul-bytes
Allow nul bytes
- Loading branch information
Showing
6 changed files
with
40 additions
and
27 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 |
---|---|---|
|
@@ -70,4 +70,5 @@ docs/_build/ | |
# Pyenv | ||
.python-version | ||
|
||
.pytest_cache/ | ||
simplet2s/_native* |
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 |
---|---|---|
|
@@ -2,13 +2,17 @@ | |
name = "simplet2s-capi" | ||
version = "0.1.5" | ||
authors = ["Messense Lv <[email protected]>"] | ||
publish = false | ||
|
||
[dependencies] | ||
c_fixed_string = "0.2" | ||
|
||
[dependencies.simplet2s] | ||
version = "0.1" | ||
path = "../" | ||
|
||
[build-dependencies] | ||
cbindgen = "0.5" | ||
cbindgen = "0.6" | ||
|
||
[lib] | ||
name = "simplet2s" | ||
|
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,28 @@ | ||
extern crate simplet2s; | ||
extern crate c_fixed_string; | ||
|
||
use std::ffi::{CStr, CString}; | ||
use std::os::raw::c_char; | ||
use std::mem; | ||
|
||
use c_fixed_string::{CFixedStr, CFixedString}; | ||
|
||
/// Frees a C str. | ||
#[no_mangle] | ||
pub unsafe extern "C" fn simplet2s_str_free(s: *mut c_char) { | ||
pub unsafe extern "C" fn simplet2s_str_free(s: *mut c_char, len: usize) { | ||
if !s.is_null() { | ||
CString::from_raw(s); | ||
CFixedString::from_raw_parts(s, len); | ||
} | ||
} | ||
|
||
#[no_mangle] | ||
pub unsafe extern "C" fn simplet2s_convert(s: *const c_char) -> *mut c_char { | ||
let s = CStr::from_ptr(s).to_str().unwrap(); | ||
let mut r = simplet2s::convert(s); | ||
pub unsafe extern "C" fn simplet2s_convert(s: *const c_char, len: usize) -> *mut c_char { | ||
let c_str = CFixedStr::from_ptr(s, len); | ||
let s = String::from_utf8_lossy(c_str.as_bytes_full()); | ||
let mut r = simplet2s::convert(&s); | ||
r.shrink_to_fit(); | ||
let c_str = CString::new(r).unwrap(); | ||
c_str.into_raw() | ||
let bytes = r.into_bytes(); | ||
let mut c_str = CFixedString::new(bytes); | ||
let ptr = c_str.as_mut_ptr(); | ||
mem::forget(c_str); | ||
ptr | ||
} |
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