Giter VIP home page Giter VIP logo

itoa's People

Contributors

adetaylor avatar anforowicz avatar dtolnay avatar glandium avatar henninglive avatar justformaven avatar kogia-sima avatar marwes avatar nox avatar object905 avatar samuelmarks avatar simonsapin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

itoa's Issues

Expose API to write to user-provided buffer

I need to pass the string that itoa returns to C, which means I need to add a null byte at the end. But there's no way to do that with the &str that format returns, short of copying the string's contents to an entirely new buffer.

If itoa exposed a method that accepted a pointer or reference to a user-created MaybeUninit buffer of the right size, I would be able to do this easily (just set the index one past the number of bytes that were written to 0).

Make Rust's `write!` faster

This crate should not be necessary. It should be possible to optimize write!(&mut buf, "{}", value) to do what this crate does.

No std example

Would it be possible to create an example for when using nostd? It works great using itoa::write with std, but I get the following error using itoa::fmt in nostd:

the trait 'core::fmt::Write' is not implemented for '[u8]'

on this line:

let n = itoa::fmt(buffer[..], num).unwrap();

Thanks :)

128 max lengths

Hi was just passing through and might have spotted something slightly amiss.

I agree that i64 and u64 has different max lengths (19, 20) but I don't believe this is the case for 128:

itoa/src/lib.rs

Line 354 in ac8183c

const U128_MAX_LEN: usize = 39;

Shouldn't they both be 39?

(also if we're there we should probably make that other constant only available with the feature)

(If you agree I'll raise a PR to adjust.)

Support for prefix/postfixed buffer

Most of the times, the buffer has prefix/postfix strings and you need to append/prepend a number to it. An API comparable to this would be ideal:

fn demo_itoa_fmt() -> fmt::Result {
    // Write to a string.
    let mut s = String::from("Result is: ");
    itoa::fmt_append(&mut s, 128u64)?;
    println!("{}", s); 

    Ok(())
}

Return io::Result<usize> instead of io::Result<()>

There is no way to know how many bytes were written in the result, which is a problem when using write with a stack-allocated array. Returning io::Result<usize> would allow this:

let mut buf = [b'\0'; 20];
let len = itoa::write(&mut buf, value)?;
let result = &buf[0..len];

Expose a safe way to efficiently write to an uninitialized, fixed buffer

Currently if I need to write an integer to a fixed size buffer I need to lookup how large of a buffer I need for the integer size and create it manually, either with zero initialization or unsafe. Further this buffer will not be directly written to as the crate creates its own buffer.

If the crate exposed a buffer type like https://docs.rs/ryu/0.2.6/ryu/struct.Buffer.html this could be avoided and hidden behind a safe interface.

pub trait Integer {
    const MAX_SIZE: usize;
}
pub struct Buffer<I> where I: Integer { buf : [u8: I::MAX_SIZE] }

Since associated constants are a relatively new Rust feature this could need some build.rs trickery and/or an extension trait but I'd be willing to submit a PR if this seems reasonable.

Support other radix

I'm interested in hexadecimal output in the same vein as itoa::fmt, and to do that I've extracted the code from libcore.

The C function itoa takes an argument for the radix, so I thought it would make sense to add the code to this crate, if you're interested. The question is what should the API look like. libcore provides a few traits for Binary, Octal, and Hexa, I think it would make sense to expose similarly named traits with the same functions as in the itoa::Integer trait.

What do you think?

itoa::Integer trait bounds

The Integer trait should probably have some bounds making to more convenient to use in generic code. I suggest adding at least Sized, Clone and Copy. We could also add math ops traits, but if you need this you should probably be using the num crate. For the other traits implemented by integers, I don’t know. The std::fmt traits might make sense, but they are not no-std if we want support that later.

Add support for 128-bit integers

Rust’s 128-bit integer types are currently unstable and feature gated behind the i128_type and i128 gates. It would be great if this crate supported them on nightly until they eventually become stabilized.

`mem::uninitialized` backport

Before 1.0.0, itoa used std::mem::uninitialized to use uninitialized u8s. This is now regarded as being undefined behaviour, and itoa correctly switched to MaybeUninit instead some time ago. But this switch came with a new major version, meaning crates that still depend on old itoa (like the current published version of csv) do not get that soundness fix.

See: rust-lang/rust#66151 for more info around mem::uninitialized.

Therefore, I would suggest backporting a soundness fix for 0.4.x. We can't use MaybeUninit, since that would increase the MSRV which is probably not desired. I would suggest to zero-initialize it instead. While this can come at a minor perf impact, it's for an old and outdated version, and most people will likely have upgrade their dependencies already. This would also be a really simple change.

What's your opinion on this? Is this something we should do right now? Maybe at a later point in the future?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.