Skip to content

Implement core::str::from_cstr_len with a test, close issue #1666 #1814

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

Merged
merged 2 commits into from
Feb 12, 2012

Conversation

uasi
Copy link
Contributor

@uasi uasi commented Feb 12, 2012

No description provided.

brson added a commit that referenced this pull request Feb 12, 2012
Implement core::str::from_cstr_len with a test, close issue #1666
@brson brson merged commit 0dacefb into rust-lang:master Feb 12, 2012
@brson
Copy link
Contributor

brson commented Feb 12, 2012

Thanks! If you want, this function could be implemented more efficiently with sys::memcpy. Something vaguely like

let buf: [u8] = [];
vec::reserve(buf, len + 1);
vec::as_buf(buf) {|buf| sys::memcpy(buf, sbuf, len) };
vec::unsafe::set_len(buf, len);
buf += [0u8];
let s = unsafe::reinterpret_cast(buf);
unsafe::leak(buf);
ret s;

This is two allocations (the initial [] and the call to reserve) and a memcpy. In the current implementation the vector will be reallocated each time its size hits a power of two and from_bytes will copy the entire buffer again.

@brson
Copy link
Contributor

brson commented Feb 12, 2012

That's ptr::memcpy, not sys

@uasi
Copy link
Contributor Author

uasi commented Feb 12, 2012

Okay, I'll give it a try. If it works I'll make another pull req.

celinval pushed a commit to celinval/rust-dev that referenced this pull request Jun 4, 2024
Kobzol pushed a commit to Kobzol/rust that referenced this pull request Dec 30, 2024
bors pushed a commit to rust-lang-ci/rust that referenced this pull request Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants