Skip to content

Commit a612e49

Browse files
Kimundibrson
authored andcommitted
Converted the floating point types to the new string conversion functions.
Also fixed all conflicting calls of the old functions in the rest of the codebase. The set of string conversion functions for each float type now consists of those items: - to_str(), converts to number in base 10 - to_str_hex(), converts to number in base 16 - to_str_radix(), converts to number in given radix - to_str_exact(), converts to number in base 10 with a exact number of trailing digits - to_str_digits(), converts to number in base 10 with a maximum number of trailing digits - implementations for to_str::ToStr and num::ToStrRadix - from_str(), parses a string as number in base 10 including decimal exponent and special values - from_str_hex(), parses a string as a number in base 16 including binary exponent and special values - from_str_radix(), parses a string as a number in a given base excluding any exponent and special values - implementations for from_str::FromStr and num::FromStrRadix
1 parent 7113fd1 commit a612e49

File tree

7 files changed

+616
-225
lines changed

7 files changed

+616
-225
lines changed

src/libcore/extfmt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ pub mod rt {
563563
pub pure fn conv_float(cv: Conv, f: float) -> ~str {
564564
let (to_str, digits) = match cv.precision {
565565
CountIs(c) => (float::to_str_exact, c as uint),
566-
CountImplied => (float::to_str, 6u)
566+
CountImplied => (float::to_str_digits, 6u)
567567
};
568568
let mut s = unsafe { to_str(f, digits) };
569569
if 0.0 <= f {

src/libcore/num/f32.rs

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ use cmath;
1818
use cmp;
1919
use libc::{c_float, c_int};
2020
use num;
21+
use option::Option;
22+
use from_str;
23+
use to_str;
2124

2225
pub use cmath::c_float_targ_consts::*;
2326

@@ -333,6 +336,197 @@ impl f32: num::Round {
333336
}
334337
}
335338

339+
/**
340+
* Section: String Conversions
341+
*/
342+
343+
/**
344+
* Converts a float to a string
345+
*
346+
* # Arguments
347+
*
348+
* * num - The float value
349+
*/
350+
#[inline(always)]
351+
pub pure fn to_str(num: f32) -> ~str {
352+
let (r, _) = num::to_str_common(
353+
&num, 10u, true, true, num::SignNeg, num::DigAll);
354+
r
355+
}
356+
357+
/**
358+
* Converts a float to a string in hexadecimal format
359+
*
360+
* # Arguments
361+
*
362+
* * num - The float value
363+
*/
364+
#[inline(always)]
365+
pub pure fn to_str_hex(num: f32) -> ~str {
366+
let (r, _) = num::to_str_common(
367+
&num, 16u, true, true, num::SignNeg, num::DigAll);
368+
r
369+
}
370+
371+
/**
372+
* Converts a float to a string in a given radix
373+
*
374+
* # Arguments
375+
*
376+
* * num - The float value
377+
* * radix - The base to use
378+
*/
379+
#[inline(always)]
380+
pub pure fn to_str_radix(num: f32, rdx: uint) -> ~str {
381+
let (r, _) = num::to_str_common(
382+
&num, rdx, true, true, num::SignNeg, num::DigAll);
383+
r
384+
}
385+
386+
/**
387+
* Converts a float to a string with exactly the number of
388+
* provided significant digits
389+
*
390+
* # Arguments
391+
*
392+
* * num - The float value
393+
* * digits - The number of significant digits
394+
*/
395+
#[inline(always)]
396+
pub pure fn to_str_exact(num: f32, dig: uint) -> ~str {
397+
let (r, _) = num::to_str_common(
398+
&num, 10u, true, true, num::SignNeg, num::DigExact(dig));
399+
r
400+
}
401+
402+
/**
403+
* Converts a float to a string with a maximum number of
404+
* significant digits
405+
*
406+
* # Arguments
407+
*
408+
* * num - The float value
409+
* * digits - The number of significant digits
410+
*/
411+
#[inline(always)]
412+
pub pure fn to_str_digits(num: f32, dig: uint) -> ~str {
413+
let (r, _) = num::to_str_common(
414+
&num, 10u, true, true, num::SignNeg, num::DigMax(dig));
415+
r
416+
}
417+
418+
impl f32: to_str::ToStr {
419+
#[inline(always)]
420+
pure fn to_str() -> ~str { to_str_digits(self, 8) }
421+
}
422+
423+
impl f32: num::ToStrRadix {
424+
#[inline(always)]
425+
pure fn to_str_radix(&self, rdx: uint) -> ~str {
426+
to_str_radix(*self, rdx)
427+
}
428+
}
429+
430+
/**
431+
* Convert a string in base 10 to a float.
432+
* Accepts a optional decimal exponent.
433+
*
434+
* This function accepts strings such as
435+
*
436+
* * '3.14'
437+
* * '+3.14', equivalent to '3.14'
438+
* * '-3.14'
439+
* * '2.5E10', or equivalently, '2.5e10'
440+
* * '2.5E-10'
441+
* * '.' (understood as 0)
442+
* * '5.'
443+
* * '.5', or, equivalently, '0.5'
444+
* * '+inf', 'inf', '-inf', 'NaN'
445+
*
446+
* Leading and trailing whitespace represent an error.
447+
*
448+
* # Arguments
449+
*
450+
* * num - A string
451+
*
452+
* # Return value
453+
*
454+
* `none` if the string did not represent a valid number. Otherwise,
455+
* `Some(n)` where `n` is the floating-point number represented by `num`.
456+
*/
457+
#[inline(always)]
458+
pub pure fn from_str(num: &str) -> Option<f32> {
459+
num::from_str_common(num, 10u, true, true, true, num::ExpDec, false)
460+
}
461+
462+
/**
463+
* Convert a string in base 16 to a float.
464+
* Accepts a optional binary exponent.
465+
*
466+
* This function accepts strings such as
467+
*
468+
* * 'a4.fe'
469+
* * '+a4.fe', equivalent to 'a4.fe'
470+
* * '-a4.fe'
471+
* * '2b.aP128', or equivalently, '2b.ap128'
472+
* * '2b.aP-128'
473+
* * '.' (understood as 0)
474+
* * 'c.'
475+
* * '.c', or, equivalently, '0.c'
476+
* * '+inf', 'inf', '-inf', 'NaN'
477+
*
478+
* Leading and trailing whitespace represent an error.
479+
*
480+
* # Arguments
481+
*
482+
* * num - A string
483+
*
484+
* # Return value
485+
*
486+
* `none` if the string did not represent a valid number. Otherwise,
487+
* `Some(n)` where `n` is the floating-point number represented by `[num]`.
488+
*/
489+
#[inline(always)]
490+
pub pure fn from_str_hex(num: &str) -> Option<f32> {
491+
num::from_str_common(num, 16u, true, true, true, num::ExpBin, false)
492+
}
493+
494+
/**
495+
* Convert a string in an given base to a float.
496+
*
497+
* Due to possible conflicts, this function does **not** accept
498+
* the special values `inf`, `-inf`, `+inf` and `NaN`, **nor**
499+
* does it recognize exponents of any kind.
500+
*
501+
* Leading and trailing whitespace represent an error.
502+
*
503+
* # Arguments
504+
*
505+
* * num - A string
506+
* * radix - The base to use. Must lie in the range [2 .. 36]
507+
*
508+
* # Return value
509+
*
510+
* `none` if the string did not represent a valid number. Otherwise,
511+
* `Some(n)` where `n` is the floating-point number represented by `num`.
512+
*/
513+
#[inline(always)]
514+
pub pure fn from_str_radix(num: &str, rdx: uint) -> Option<f32> {
515+
num::from_str_common(num, rdx, true, true, false, num::ExpNone, false)
516+
}
517+
518+
impl f32: from_str::FromStr {
519+
#[inline(always)]
520+
static pure fn from_str(val: &str) -> Option<f32> { from_str(val) }
521+
}
522+
523+
impl f32: num::FromStrRadix {
524+
#[inline(always)]
525+
static pure fn from_str_radix(val: &str, rdx: uint) -> Option<f32> {
526+
from_str_radix(val, rdx)
527+
}
528+
}
529+
336530
//
337531
// Local Variables:
338532
// mode: rust

0 commit comments

Comments
 (0)