// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your // option. This file may not be copied, modified, or distributed // except according to those terms. // // --- // // The C++ implementation preserved here in comments is licensed as follows: // // Tencent is pleased to support the open source community by making RapidJSON // available. // // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All // rights reserved. // // Licensed under the MIT License (the "License"); you may not use this file // except in compliance with the License. You may obtain a copy of the License // at // // http://opensource.org/licenses/MIT // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations under // the License.
/* inlineunsignedCountDecimalDigit32(uint32_tn){ // Simple pure C++ implementation was faster than __builtin_clz version in this situation. if(n<10)return1; if(n<100)return2; if(n<1000)return3; if(n<10000)return4; if(n<100000)return5; if(n<1000000)return6; if(n<10000000)return7; if(n<100000000)return8; // Will not reach 10 digits in DigitGen() //if (n < 1000000000) return 9; //return 10; return9; }
*/
#[inline] fn count_decimal_digit32(n: u32) -> usize { if n < 10 { 1 } elseif n < 100 { 2 } elseif n < 1000 { 3 } elseif n < 10000 { 4 } elseif n < 100000 { 5 } elseif n < 1000000 { 6 } elseif n < 10000000 { 7 } elseif n < 100000000 { 8 } // Will not reach 10 digits in digit_gen() else { 9 }
}
// Returns length and k. #[inline] unsafefn grisu2(value: $fty, buffer: *mut u8) -> (isize, isize) { let v = DiyFp::from(value); let (w_m, w_p) = v.normalized_boundaries();
#[inline] unsafefn write_exponent(mut k: isize, mut buffer: *mut u8) -> *mut u8 { if k < 0 {
*buffer = b'-';
buffer = buffer.offset(1);
k = -k;
}
if k >= 100 {
*buffer = b'0' + (k / 100) as u8;
k %= 100; let d = DEC_DIGITS_LUT.get_unchecked(k as usize * 2);
ptr::copy_nonoverlapping(d, buffer.offset(1), 2);
buffer.offset(3)
} elseif k >= 10 { let d = DEC_DIGITS_LUT.get_unchecked(k as usize * 2);
ptr::copy_nonoverlapping(d, buffer, 2);
buffer.offset(2)
} else {
*buffer = b'0' + k as u8;
buffer.offset(1)
}
}
/* inlinechar*Prettify(char*buffer,intlength,intk,intmaxDecimalPlaces){ constintkk=length+k;// 10^(kk-1) <= v < 10^kk
*/
#[inline] unsafefn prettify(buffer: *mut u8, length: isize, k: isize) -> *mut u8 { let kk = length + k; // 10^(kk-1) <= v < 10^kk
/* if(0<=k&&kk<=21){ // 1234e7 -> 12340000000 for(inti=length;i<kk;i++) buffer[i]='0'; buffer[kk]='.'; buffer[kk+1]='0'; return&buffer[kk+2]; }
*/ if0 <= k && kk <= 21 { // 1234e7 -> 12340000000 for i in length..kk {
*buffer.offset(i) = b'0';
}
*buffer.offset(kk) = b'.';
*buffer.offset(kk + 1) = b'0';
buffer.offset(kk + 2)
}
/* elseif(0<kk&&kk<=21){ // 1234e-2 -> 12.34 std::memmove(&buffer[kk+1],&buffer[kk],static_cast<size_t>(length-kk)); buffer[kk]='.'; if(0>k+maxDecimalPlaces){ // When maxDecimalPlaces = 2, 1.2345 -> 1.23, 1.102 -> 1.1 // Remove extra trailing zeros (at least one) after truncation. for(inti=kk+maxDecimalPlaces;i>kk+1;i--) if(buffer[i]!='0') return&buffer[i+1]; return&buffer[kk+2];// Reserve one zero } else return&buffer[length+1]; }
*/ elseif0 < kk && kk <= 21 { // 1234e-2 -> 12.34
ptr::copy(buffer.offset(kk), buffer.offset(kk + 1), (length - kk) as usize);
*buffer.offset(kk) = b'.'; if0 > k + MAX_DECIMAL_PLACES { // When MAX_DECIMAL_PLACES = 2, 1.2345 -> 1.23, 1.102 -> 1.1 // Remove extra trailing zeros (at least one) after truncation. for i in (kk + 2 .. kk + MAX_DECIMAL_PLACES + 1).rev() { if *buffer.offset(i) != b'0' { return buffer.offset(i + 1);
}
}
buffer.offset(kk + 2) // Reserve one zero
} else {
buffer.offset(length + 1)
}
}
/* elseif(-6<kk&&kk<=0){ // 1234e-6 -> 0.001234 constintoffset=2-kk; std::memmove(&buffer[offset],&buffer[0],static_cast<size_t>(length)); buffer[0]='0'; buffer[1]='.'; for(inti=2;i<offset;i++) buffer[i]='0'; if(length-kk>maxDecimalPlaces){ // When maxDecimalPlaces = 2, 0.123 -> 0.12, 0.102 -> 0.1 // Remove extra trailing zeros (at least one) after truncation. for(inti=maxDecimalPlaces+1;i>2;i--) if(buffer[i]!='0') return&buffer[i+1]; return&buffer[3];// Reserve one zero } else return&buffer[length+offset]; }
*/ elseif -6 < kk && kk <= 0 { // 1234e-6 -> 0.001234 let offset = 2 - kk;
ptr::copy(buffer, buffer.offset(offset), length as usize);
*buffer = b'0';
*buffer.offset(1) = b'.'; for i in2..offset {
*buffer.offset(i) = b'0';
} if length - kk > MAX_DECIMAL_PLACES { // When MAX_DECIMAL_PLACES = 2, 0.123 -> 0.12, 0.102 -> 0.1 // Remove extra trailing zeros (at least one) after truncation. for i in (3 .. MAX_DECIMAL_PLACES + 2).rev() { if *buffer.offset(i) != b'0' { return buffer.offset(i + 1);
}
}
buffer.offset(3) // Reserve one zero
} else {
buffer.offset(length + offset)
}
}
/* elseif(kk<-maxDecimalPlaces){ // Truncate to zero buffer[0]='0'; buffer[1]='.'; buffer[2]='0'; return&buffer[3]; }
*/ elseif kk < -MAX_DECIMAL_PLACES {
*buffer = b'0';
*buffer.offset(1) = b'.';
*buffer.offset(2) = b'0';
buffer.offset(3)
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.