Line data Source code
1 : /* File: utf8string.inl; Copyright and License: see below */ 2 : 3 : #include "utf8stringbuf/utf8codepoint.h" 4 : #include <string.h> 5 : #include <stdio.h> 6 : #include <stdint.h> 7 : #include <stdlib.h> 8 : #include <errno.h> 9 : 10 : #ifdef __cplusplus 11 : extern "C" { 12 : #endif 13 : 14 : /*! 15 : * \enum utf8string_bool_enum 16 : * \private 17 : */ 18 : /* enumeration for true and false or success and failure */ 19 : enum utf8string_bool_enum {UTF8STRING_FALSE=0, UTF8STRING_TRUE=1,}; 20 : 21 : /*! 22 : * \enum utf8string_search_enum 23 : * \private 24 : */ 25 : /* enumeration for search pattern not found */ 26 : enum utf8string_search_enum {UTF8STRING_NOT_FOUND=-1,}; 27 : 28 38 : static inline size_t utf8string_get_size( utf8string_t *this_ ) { 29 38 : size_t sizeResult = 0; 30 38 : if ( this_ != NULL ) { 31 34 : sizeResult = strlen( this_ ) + 1; /* 1 for size of terminating 0 character */ 32 : } 33 38 : return sizeResult; 34 : } 35 : 36 156 : static inline unsigned int utf8string_get_length( utf8string_t *this_ ) { 37 156 : unsigned int lenResult = 0; 38 156 : if ( this_ != NULL ) { 39 155 : lenResult = strlen( this_ ); 40 : } 41 156 : return lenResult; 42 : } 43 : 44 92 : static inline int utf8string_equals_str( utf8string_t *this_, utf8string_t *that ) { 45 92 : int cmpResult = -1; 46 92 : if (( this_ != NULL ) && ( that != NULL )) { 47 89 : cmpResult = strcmp( this_, that ); 48 : } 49 92 : return ( cmpResult == 0 ) ? UTF8STRING_TRUE : UTF8STRING_FALSE; 50 : } 51 : 52 : #ifdef UTF8STRING_DEPRECATED_INDEX 53 10 : static inline int utf8string_equals_region_str( utf8string_t *this_, int start, utf8string_t *that ) { 54 10 : int cmpResult = -1; 55 10 : if (( this_ != NULL ) && ( that != NULL )) { 56 6 : int thisLen = strlen(this_); 57 6 : int thatLen = strlen(that); 58 6 : unsigned int end = ((unsigned int)start) + ((unsigned int)thatLen); 59 6 : if (( 0 <= start )&&( end <= thisLen )) { 60 6 : cmpResult = memcmp( &(this_[start]), that, thatLen ); 61 : } 62 : } 63 10 : return ( cmpResult == 0 ) ? UTF8STRING_TRUE : UTF8STRING_FALSE; 64 : } 65 : #endif /* UTF8STRING_DEPRECATED_INDEX */ 66 : 67 54 : static inline int utf8string_starts_with_str( utf8string_t *this_, utf8string_t *that ) { 68 54 : int cmpResult = -1; 69 54 : if (( this_ != NULL )&&( that != NULL )) { 70 51 : unsigned int thatLen = strlen( that ); 71 51 : cmpResult = strncmp( this_, that, thatLen ); 72 : } 73 54 : return ( cmpResult == 0 ) ? UTF8STRING_TRUE : UTF8STRING_FALSE; 74 : } 75 : 76 14 : static inline int utf8string_ends_with_str( utf8string_t *this_, utf8string_t *that ) { 77 14 : int cmpResult = -1; 78 14 : if (( this_ != NULL )&&( that != NULL )) { 79 11 : unsigned int thatLen = strlen( that ); 80 11 : unsigned int thisLen = strlen( this_ ); 81 11 : if ( thatLen <= thisLen ) { 82 10 : cmpResult = memcmp( &(this_[thisLen-thatLen]), that, thatLen ); 83 : } 84 : } 85 14 : return ( cmpResult == 0 ) ? UTF8STRING_TRUE : UTF8STRING_FALSE; 86 : } 87 : 88 7 : static inline int utf8string_find_first_str( utf8string_t *this_, utf8string_t *pattern ) { 89 7 : int result = UTF8STRING_NOT_FOUND; 90 7 : if (( pattern != NULL )&&( this_ != NULL )) { 91 4 : const char *ptrResult = strstr( this_, pattern ); 92 4 : if ( ptrResult != NULL ) { 93 2 : result = (int) (ptrResult - this_); 94 : } 95 : } 96 7 : return result; 97 : } 98 : 99 5 : static inline int utf8string_find_last_str( utf8string_t *this_, utf8string_t *pattern ) { 100 5 : int result = UTF8STRING_NOT_FOUND; 101 5 : if (( pattern != NULL )&&( this_ != NULL )) { 102 3 : int thisLen = strlen( this_ ); 103 3 : int patternLen = strlen( pattern ); 104 3 : if ( patternLen <= thisLen ) { 105 8 : for ( int probeIdx = (thisLen-patternLen); probeIdx >= 0; probeIdx --) { 106 7 : if ( 0 == memcmp( &(this_[probeIdx]), pattern, patternLen )) { 107 : /* last occurrence found! */ 108 2 : result = probeIdx; 109 2 : break; 110 : } 111 : } 112 : } 113 : } 114 5 : return result; 115 : } 116 : 117 10 : static inline int utf8string_find_next_str( utf8string_t *this_, utf8string_t *pattern, int start_index ) { 118 10 : int result = UTF8STRING_NOT_FOUND; 119 10 : unsigned int thisSize = utf8string_get_size( this_ ); 120 10 : if (( pattern != NULL ) && ( start_index >= 0 ) && ( start_index < thisSize )) { 121 8 : const char *ptrResult = strstr( &(this_[start_index]), pattern ); 122 8 : if ( ptrResult != NULL ) { 123 6 : result = (int) (ptrResult - this_); 124 : } 125 : } 126 10 : return result; 127 : } 128 : 129 22 : static inline utf8codepoint_t utf8string_get_char_at( utf8string_t *this_, unsigned int byte_index ) { 130 22 : utf8codepoint_t result = UTF8CODEPOINT_INVAL_CHAR; 131 22 : unsigned int thisSize = utf8string_get_size( this_ ); 132 22 : if ( byte_index < thisSize ) { 133 20 : result = utf8codepoint_init( &(this_[byte_index]), thisSize-byte_index ); 134 : } 135 22 : return result; 136 : } 137 : 138 : #ifdef __cplusplus 139 : } 140 : #endif 141 : 142 : 143 : /* 144 : * Copyright 2012-2024 Andreas Warnke 145 : * 146 : * Licensed under the Apache License, Version 2.0 (the "License"); 147 : * you may not use this file except in compliance with the License. 148 : * You may obtain a copy of the License at 149 : * 150 : * http://www.apache.org/licenses/LICENSE-2.0 151 : * 152 : * Unless required by applicable law or agreed to in writing, software 153 : * distributed under the License is distributed on an "AS IS" BASIS, 154 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 155 : * See the License for the specific language governing permissions and 156 : * limitations under the License. 157 : */