Line data Source code
1 : /* File: utf8stringview.c; Copyright and License: see below */ 2 : 3 : #include "utf8stringbuf/utf8stringview.h" 4 : #include "utf8stringbuf/utf8stringbuf.h" 5 : #include "utf8stringbuf/utf8string.h" 6 : 7 : #ifdef __cplusplus 8 : extern "C" { 9 : #endif 10 : 11 27 : utf8error_t utf8stringview_parse_int( const utf8stringview_t *this_, int64_t *out_number, utf8stringview_t *out_remainder ) 12 : { 13 27 : assert( out_number != NULL ); 14 27 : utf8error_t result = UTF8ERROR_SUCCESS; 15 : 16 : /* the maximum int64 decimal presentation is 20 characters: -9223372036854775808 */ 17 : /* adding some buffer for leading zeros and spaces... */ 18 27 : char number_arr[40] = ""; 19 27 : utf8stringbuf_t number_buf = UTF8STRINGBUF( number_arr ); 20 27 : result |= utf8stringbuf_copy_view( number_buf, this_ ); 21 : 22 27 : unsigned int byte_length = 0; 23 27 : result |= utf8string_parse_int( utf8stringbuf_get_string( number_buf ), &byte_length, out_number ); 24 27 : assert( byte_length <= (*this_).length ); 25 : 26 27 : if ( out_remainder != NULL ) 27 : { 28 26 : *out_remainder = (utf8stringview_t){ .start = (*this_).start + byte_length, .length = (*this_).length - byte_length }; 29 : } 30 27 : return result; 31 : } 32 : 33 4 : utf8error_t utf8stringview_parse_float( const utf8stringview_t *this_, double *out_number, utf8stringview_t *out_remainder ) 34 : { 35 4 : assert( out_number != NULL ); 36 4 : utf8error_t result = UTF8ERROR_SUCCESS; 37 : 38 : /* the maximum double 64 decimal presentation in e-notation is 23 characters: -2.2250738585072014E-308 */ 39 : /* adding some buffer for leading zeros, spaces, additional accuracy and non-e-notation */ 40 4 : char number_arr[80] = ""; 41 4 : utf8stringbuf_t number_buf = UTF8STRINGBUF( number_arr ); 42 4 : result |= utf8stringbuf_copy_view( number_buf, this_ ); 43 : 44 4 : unsigned int byte_length = 0; 45 4 : result |= utf8string_parse_float( utf8stringbuf_get_string( number_buf ), &byte_length, out_number ); 46 4 : assert( byte_length <= (*this_).length ); 47 : 48 4 : if ( out_remainder != NULL ) 49 : { 50 3 : *out_remainder = (utf8stringview_t){ .start = (*this_).start + byte_length, .length = (*this_).length - byte_length }; 51 : } 52 4 : return result; 53 : } 54 : 55 : #ifdef __cplusplus 56 : } 57 : #endif 58 : 59 : 60 : /* 61 : * Copyright 2024-2024 Andreas Warnke 62 : * 63 : * Licensed under the Apache License, Version 2.0 (the "License"); 64 : * you may not use this file except in compliance with the License. 65 : * You may obtain a copy of the License at 66 : * 67 : * http://www.apache.org/licenses/LICENSE-2.0 68 : * 69 : * Unless required by applicable law or agreed to in writing, software 70 : * distributed under the License is distributed on an "AS IS" BASIS, 71 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 72 : * See the License for the specific language governing permissions and 73 : * limitations under the License. 74 : */