forked from kimhyunkang/rust-gmp
-
Couldn't load subscription status.
- Fork 27
Mpf get str fixups #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bryant
wants to merge
10
commits into
fizyk20:master
Choose a base branch
from
bryant:mpf-get-str-fixups
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2370a3c
add wrapper for gmp-allocated strings
bryant 4d797ca
__gmpf_get_str is const on last arg.
bryant e2c9bde
properly call mpf_get_str
bryant 0ebc599
mpf_get_str's first arg is mut.
bryant f90d082
rework the known n_digits case.
bryant 68108f5
pass null_mut
bryant 12cf1c9
initialize the Vec with zeros.
bryant d820198
replace use of transmute with safe cast.
bryant 3bcc529
don't include null bytes from mpf_get_str.
bryant 58d47c2
test get_str.
bryant File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,7 +1,38 @@ | ||
| use mpz::*; | ||
| use std::{mem, slice, str}; | ||
| use libc::{c_char, strlen}; | ||
|
|
||
| type AllocFunc = extern "C" fn(usize) -> *mut c_char; | ||
| type ReallocFunc = extern "C" fn(*mut c_char, usize, usize) -> *mut c_char; | ||
| type FreeFunc = extern "C" fn(*mut c_char, usize); | ||
|
|
||
| #[link(name = "gmp")] | ||
| extern "C" { | ||
| pub fn __gmpz_fdiv_q(q: mpz_ptr, n: mpz_srcptr, d: mpz_srcptr); | ||
| pub fn __gmpz_cdiv_q(q: mpz_ptr, n: mpz_srcptr, d: mpz_srcptr); | ||
| pub fn __gmp_get_memory_functions(alloc: *mut AllocFunc, relloc: *mut ReallocFunc, free: *mut FreeFunc); | ||
| } | ||
|
|
||
| pub struct GString(*mut u8, usize); | ||
|
|
||
| impl GString { | ||
| pub unsafe fn from_raw(raw: *mut c_char) -> GString { | ||
| GString(raw as *mut u8, strlen(raw) as usize + 1) | ||
| } | ||
|
|
||
| pub fn to_str(&self) -> Result<&str, str::Utf8Error> { | ||
| let bytes: &[u8] = unsafe { slice::from_raw_parts(self.0, self.1) }; | ||
| str::from_utf8(&bytes[..bytes.len() - 1]) | ||
| } | ||
| } | ||
|
|
||
| impl Drop for GString { | ||
| fn drop(&mut self) { | ||
| use std::ptr::null_mut; | ||
| unsafe { | ||
| let mut free_func: FreeFunc = mem::uninitialized(); | ||
| __gmp_get_memory_functions(null_mut(), null_mut(), &mut free_func); | ||
| free_func(self.0 as *mut c_char, self.1); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CStr::from_ptris more direct and avoids the separatestrlen.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no matter what, the length needs to be known in order to call the free func, so it's either strlen or CStr::to_bytes().len(). i think strlen communicates more clearly how the length param to free_func is computed, whereas the alternate method relies on knowing that CStr implicitly calls strlen behind the scenes.