1919/// reference:
2020/// - <https://nigeltao.github.io/blog/2020/parse-number-f64-simple.html>
2121/// - <https://nigeltao.github.io/blog/2020/eisel-lemire.html>
22+ /// @alert deprecated "Decimal will be changed to private. Use `@strconv.parse_double` instead"
2223struct Decimal {
2324 digits : FixedArray [Byte ]
2425 mut digits_num : Int
@@ -41,7 +42,13 @@ let powtab = [
4142
4243///|
4344/// Create a zero decimal.
45+ /// @alert deprecated "Decimal will be changed to private. Use `@strconv.parse_double` instead"
4446pub fn Decimal ::new () -> Decimal {
47+ Decimal ::new_priv ()
48+ }
49+
50+ ///|
51+ fn Decimal ::new_priv () -> Decimal {
4552 {
4653 digits : FixedArray ::make (800 , Byte ::default ()),
4754 digits_num : 0 ,
@@ -53,8 +60,14 @@ pub fn Decimal::new() -> Decimal {
5360
5461///|
5562/// Create a decimal with an Int64 value.
63+ /// @alert deprecated "Decimal will be changed to private. Use `@strconv.parse_double` instead"
5664pub fn Decimal ::from_int64 (v : Int64 ) -> Decimal {
57- Decimal ::new ()..assign (v )
65+ Decimal ::from_int64_priv (v )
66+ }
67+
68+ ///|
69+ fn Decimal ::from_int64_priv (v : Int64 ) -> Decimal {
70+ Decimal ::new_priv ()..assign (v )
5871}
5972
6073///|
@@ -78,13 +91,19 @@ pub fn Decimal::from_int64(v : Int64) -> Decimal {
7891///
7992/// An exponent value exp scales the mantissa (significand) by 10^exp.
8093/// For example, "1.23e2" represents 1.23 × 10² = 123.
94+ /// @alert deprecated "use `@strconv.parse_double` instead"
8195pub fn parse_decimal (str : String ) -> Decimal !StrConvError {
8296 parse_decimal_from_view! (str [:])
8397}
8498
99+ ///|
100+ fn parse_decimal_priv (str : String ) -> Decimal !StrConvError {
101+ parse_decimal_from_view! (str [:])
102+ }
103+
85104///|
86105fn parse_decimal_from_view (str : @string .StringView ) -> Decimal !StrConvError {
87- let d = Decimal ::new ()
106+ let d = Decimal ::new_priv ()
88107 let mut has_dp = false
89108 let mut has_digits = false
90109 // read sign
@@ -158,14 +177,20 @@ fn parse_decimal_from_view(str : @string.StringView) -> Decimal!StrConvError {
158177 d ..trim ()
159178}
160179
161- ///| @alert deprecated "use `@strconv.parse_decimal ` instead"
180+ ///| @alert deprecated "use `@strconv.parse_double ` instead"
162181pub fn Decimal ::parse_decimal (str : String ) -> Decimal !StrConvError {
163- parse_decimal ! (str )
182+ parse_decimal_from_view ! (str [:] )
164183}
165184
166185///|
167186/// Convert the decimal to Double.
187+ ///| @alert deprecated "use `@strconv.parse_double` instead to avoid using this method."
168188pub fn to_double (self : Decimal ) -> Double !StrConvError {
189+ self .to_double_priv! ()
190+ }
191+
192+ ///|
193+ fn to_double_priv (self : Decimal ) -> Double !StrConvError {
169194 let mut exponent = 0
170195 let mut mantissa = 0L
171196 // check the underflow and overflow
@@ -191,7 +216,7 @@ pub fn to_double(self : Decimal) -> Double!StrConvError {
191216 } else {
192217 n = powtab [self .decimal_point]
193218 }
194- self .shift (- n )
219+ self .shift_priv (- n )
195220 exponent + = n
196221 }
197222 // left shift
@@ -203,7 +228,7 @@ pub fn to_double(self : Decimal) -> Double!StrConvError {
203228 } else {
204229 n = powtab [- self .decimal_point]
205230 }
206- self .shift (n )
231+ self .shift_priv (n )
207232 exponent - = n
208233 }
209234
@@ -215,7 +240,7 @@ pub fn to_double(self : Decimal) -> Double!StrConvError {
215240 // if the exponent is smaller, move it up and shift decimal accordingly
216241 if exponent < double_info .bias + 1 {
217242 let n = double_info .bias + 1 - exponent
218- self .shift (- n )
243+ self .shift_priv (- n )
219244 exponent + = n
220245 }
221246 if exponent - double_info .bias >= (1 << double_info .exponent_bits) - 1 {
@@ -225,7 +250,7 @@ pub fn to_double(self : Decimal) -> Double!StrConvError {
225250
226251 // multiply by (2 ** precision) and round to get mantissa
227252 // extract mantissa_bits + 1 bits
228- self .shift (double_info .mantissa_bits + 1 )
253+ self .shift_priv (double_info .mantissa_bits + 1 )
229254 mantissa = self .rounded_integer ()
230255
231256 // rounding might have added a bit, shift down.
@@ -251,7 +276,13 @@ pub fn to_double(self : Decimal) -> Double!StrConvError {
251276///|
252277/// Binary shift left (s > 0) or right (s < 0).
253278/// The shift count must not larger than the max_shift to avoid overflow.
279+ /// @alert deprecated "use `@strconv.parse_double` instead to avoid using this method."
254280pub fn shift (self : Decimal , s : Int ) -> Unit {
281+ self .shift_priv (s )
282+ }
283+
284+ ///|
285+ fn shift_priv (self : Decimal , s : Int ) -> Unit {
255286 if self .digits_num == 0 {
256287 return
257288 }
@@ -601,18 +632,8 @@ fn Decimal::to_string(self : Decimal) -> String {
601632 Show ::to_string (self )
602633}
603634
604- test "from_int64" {
605- let d = Decimal ::from_int64 (- 1 )
606- inspect! (d .to_string (), content = "0" )
607- }
608-
609- test "to_string" {
610- let d = parse_decimal! ("-1" )
611- inspect! (d .to_string (), content = "1" )
612- }
613-
614635test "new" {
615- let hpd = Decimal ::from_int64 (1L )
636+ let hpd = Decimal ::from_int64_priv (1L )
616637 assert_eq! (hpd .digits.length (), 800 )
617638 assert_eq! (hpd .digits_num, 1 )
618639 assert_eq! (hpd .decimal_point, 1 )
@@ -621,28 +642,28 @@ test "new" {
621642}
622643
623644test "from_int64" {
624- let hpd = Decimal ::from_int64 (123456789L )
645+ let hpd = Decimal ::from_int64_priv (123456789L )
625646 assert_eq! (hpd .to_string (), "123456789" )
626647}
627648
628649test "parse_decimal" {
629650 let s = "0.0000000000000000000000000000007888609052210118054117285652827862296732064351090230047702789306640625"
630651 let hpd = try {
631- parse_decimal ! (s )
652+ parse_decimal_priv ! (s )
632653 } catch {
633654 _ => panic ()
634655 }
635656 assert_eq! (hpd .to_string (), s )
636657 let hpd = try {
637- parse_decimal ! ("1.0e-10" )
658+ parse_decimal_priv ! ("1.0e-10" )
638659 } catch {
639660 _ => panic ()
640661 }
641662 assert_eq! (hpd .to_string (), "0.0000000001" )
642663}
643664
644665test "to_string" {
645- let hpd = Decimal ::from_int64 (123456789L )
666+ let hpd = Decimal ::from_int64_priv (123456789L )
646667 hpd .decimal_point = 1
647668 assert_eq! (hpd .to_string (), "1.23456789" )
648669 hpd .decimal_point = 0
@@ -668,24 +689,24 @@ test "shift" {
668689 ]
669690 for i = 0 ; i < tests .length (); i = i + 1 {
670691 let t = tests [i ]
671- let d = Decimal ::from_int64 (t .0 )
672- d .shift (t .1 )
692+ let d = Decimal ::from_int64_priv (t .0 )
693+ d .shift_priv (t .1 )
673694 assert_eq! (d .to_string (), t .2 )
674695 }
675696}
676697
677698test "parse decimal with underscore" {
678- inspect! (parse_decimal ! ("1e1_2" ), content = "1000000000000" )
679- inspect! (parse_decimal ! ("1e12" ), content = "1000000000000" )
680- inspect! (parse_decimal ! ("1_2_3" ), content = "123" )
699+ inspect! (parse_decimal_priv ! ("1e1_2" ), content = "1000000000000" )
700+ inspect! (parse_decimal_priv ! ("1e12" ), content = "1000000000000" )
701+ inspect! (parse_decimal_priv ! ("1_2_3" ), content = "123" )
681702}
682703
683704test "parse decimal error" {
684- inspect! (parse_decimal ? ("1e" ), content = "Err(invalid syntax)" )
685- inspect! (parse_decimal ? ("1e+" ), content = "Err(invalid syntax)" )
686- inspect! (parse_decimal ? ("1e_" ), content = "Err(invalid syntax)" )
687- inspect! (parse_decimal ? ("1-23" ), content = "Err(invalid syntax)" )
688- inspect! (parse_decimal ? ("1.2.3" ), content = "Err(invalid syntax)" )
705+ inspect! (parse_decimal_priv ? ("1e" ), content = "Err(invalid syntax)" )
706+ inspect! (parse_decimal_priv ? ("1e+" ), content = "Err(invalid syntax)" )
707+ inspect! (parse_decimal_priv ? ("1e_" ), content = "Err(invalid syntax)" )
708+ inspect! (parse_decimal_priv ? ("1-23" ), content = "Err(invalid syntax)" )
709+ inspect! (parse_decimal_priv ? ("1.2.3" ), content = "Err(invalid syntax)" )
689710}
690711
691712test "parse decimal with large numbers" {
@@ -694,16 +715,16 @@ test "parse decimal with large numbers" {
694715 s + = "9"
695716 }
696717 inspect! (
697- parse_decimal ! (s ),
718+ parse_decimal_priv ! (s ),
698719 content = "9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999" ,
699720 )
700721}
701722
702723test "should_round_up when truncated and half-way" {
703724 // Create a decimal from "12.50"
704- let decimal = parse_decimal ! ("12.50" )
725+ let decimal = parse_decimal_priv ! ("12.50" )
705726 // Convert to double
706- inspect! (decimal .to_double ! (), content = "12.5" )
727+ inspect! (decimal .to_double_priv ! (), content = "12.5" )
707728}
708729
709730test "parse_decimal with large numbers and truncation" {
@@ -713,13 +734,13 @@ test "parse_decimal with large numbers and truncation" {
713734 s + = "0"
714735 }
715736 inspect! (
716- parse_decimal ! (s ),
737+ parse_decimal_priv ! (s ),
717738 content = "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" ,
718739 )
719740}
720741
721742test "corner cases" {
722- inspect! (parse_decimal ? (".123" ), content = "Ok(0.123)" )
723- inspect! (parse_decimal ? ("." ), content = "Err(invalid syntax)" )
724- inspect! (parse_decimal ? ("-" ), content = "Err(invalid syntax)" )
743+ inspect! (parse_decimal_priv ? (".123" ), content = "Ok(0.123)" )
744+ inspect! (parse_decimal_priv ? ("." ), content = "Err(invalid syntax)" )
745+ inspect! (parse_decimal_priv ? ("-" ), content = "Err(invalid syntax)" )
725746}
0 commit comments