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 : /* it is ok if not all characters could be copied, a number cannot be longer than 39 characters. */ 22 27 : result = UTF8ERROR_SUCCESS; 23 : 24 27 : unsigned int byte_length = 0; 25 27 : result |= utf8string_parse_int( utf8stringbuf_get_string( &number_buf ), &byte_length, out_number ); 26 27 : assert( byte_length <= (*this_).length ); 27 : 28 27 : if ( out_remainder != NULL ) 29 : { 30 26 : *out_remainder = (utf8stringview_t){ .start = (*this_).start + byte_length, .length = (*this_).length - byte_length }; 31 : } 32 27 : return result; 33 : } 34 : 35 4 : utf8error_t utf8stringview_parse_float( const utf8stringview_t *this_, double *out_number, utf8stringview_t *out_remainder ) 36 : { 37 4 : assert( out_number != NULL ); 38 4 : utf8error_t result = UTF8ERROR_SUCCESS; 39 : 40 : /* the maximum double 64 decimal presentation in e-notation is 23 characters: -2.2250738585072014E-308 */ 41 : /* adding some buffer for leading zeros, spaces, additional accuracy and non-e-notation */ 42 4 : char number_arr[80] = ""; 43 4 : utf8stringbuf_t number_buf = UTF8STRINGBUF( number_arr ); 44 4 : result |= utf8stringbuf_copy_view( &number_buf, this_ ); 45 : /* it is ok if not all characters could be copied, a float number cannot be longer than 79 characters. */ 46 4 : result = UTF8ERROR_SUCCESS; 47 : 48 4 : unsigned int byte_length = 0; 49 4 : result |= utf8string_parse_float( utf8stringbuf_get_string( &number_buf ), &byte_length, out_number ); 50 4 : assert( byte_length <= (*this_).length ); 51 : 52 4 : if ( out_remainder != NULL ) 53 : { 54 3 : *out_remainder = (utf8stringview_t){ .start = (*this_).start + byte_length, .length = (*this_).length - byte_length }; 55 : } 56 4 : return result; 57 : } 58 : 59 : #ifdef __cplusplus 60 : } 61 : #endif 62 : 63 : 64 : /* 65 : * Copyright 2024-2025 Andreas Warnke 66 : * 67 : * Licensed under the Apache License, Version 2.0 (the "License"); 68 : * you may not use this file except in compliance with the License. 69 : * You may obtain a copy of the License at 70 : * 71 : * http://www.apache.org/licenses/LICENSE-2.0 72 : * 73 : * Unless required by applicable law or agreed to in writing, software 74 : * distributed under the License is distributed on an "AS IS" BASIS, 75 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 76 : * See the License for the specific language governing permissions and 77 : * limitations under the License. 78 : */