1 /*
2 * Copyright (c) 2014, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "jvm_io.h"
26 #include "memory/allocation.hpp"
27 #include "runtime/os.hpp"
28 #include "utilities/debug.hpp"
29 #include "utilities/stringUtils.hpp"
30
31 #include <ctype.h>
32 #include <string.h>
33
34 int StringUtils::replace_no_expand(char* string, const char* from, const char* to) {
35 int replace_count = 0;
36 size_t from_len = strlen(from);
37 size_t to_len = strlen(to);
38 assert(from_len >= to_len, "must not expand input");
39
40 for (char* dst = string; *dst && (dst = strstr(dst, from)) != nullptr;) {
41 char* left_over = dst + from_len;
42 memmove(dst, to, to_len); // does not copy trailing 0 of <to>
43 dst += to_len; // skip over the replacement.
44 memmove(dst, left_over, strlen(left_over) + 1); // copies the trailing 0 of <left_over>
45 ++ replace_count;
46 }
47
48 return replace_count;
49 }
50
51 double StringUtils::similarity(const char* str1, size_t len1, const char* str2, size_t len2) {
52 assert(str1 != nullptr && str2 != nullptr, "sanity");
53
54 // filter out zero-length strings else we will underflow on len-1 below
55 if (len1 == 0 || len2 == 0) {
56 return 0.0;
57 }
58
59 size_t total = len1 + len2;
60 size_t hit = 0;
61
62 for (size_t i = 0; i < len1 - 1; i++) {
63 for (size_t j = 0; j < len2 - 1; j++) {
64 if ((str1[i] == str2[j]) && (str1[i+1] == str2[j+1])) {
65 ++hit;
66 break;
67 }
68 }
69 }
70
71 return 2.0 * (double) hit / (double) total;
72 }
73
74 const char* StringUtils::strstr_nocase(const char* haystack, const char* needle) {
75 if (needle[0] == '\0') {
76 return haystack; // empty needle matches with anything
77 }
78 for (size_t i = 0; haystack[i] != '\0'; i++) {
79 bool matches = true;
80 for (size_t j = 0; needle[j] != '\0'; j++) {
81 if (haystack[i + j] == '\0') {
82 return nullptr; // hit end of haystack, abort
83 }
84 if (tolower(haystack[i + j]) != tolower(needle[j])) {
85 matches = false;
86 break; // abort, try next i
87 }
88 }
89 if (matches) {
90 return &haystack[i]; // all j were ok for this i
91 }
92 }
93 return nullptr; // no i was a match
94 }
95
96 bool StringUtils::is_star_match(const char* star_pattern, const char* str) {
97 const int N = 1000;
98 char pattern[N]; // copy pattern into this to ensure null termination
99 jio_snprintf(pattern, N, "%s", star_pattern);// ensures null termination
100 char buf[N]; // copy parts of pattern into this
101 const char* str_idx = str;
102 const char* pattern_idx = pattern;
103 while (strlen(pattern_idx) > 0) {
104 // find next section in pattern
105 const char* pattern_part_end = strstr(pattern_idx, "*");
106 const char* pattern_part = pattern_idx;
107 if (pattern_part_end != nullptr) { // copy part into buffer
108 size_t pattern_part_len = pattern_part_end-pattern_part;
109 strncpy(buf, pattern_part, pattern_part_len);
110 buf[pattern_part_len] = '\0'; // end of string
111 pattern_part = buf;
112 }
113 // find this section in s, case insensitive
114 const char* str_match = strstr_nocase(str_idx, pattern_part);
115 if (str_match == nullptr) {
116 return false; // r_part did not match - abort
117 }
118 size_t match_len = strlen(pattern_part);
119 // advance to match position plus part length
120 str_idx = str_match + match_len;
121 // advance by part length and "*"
122 pattern_idx += match_len + (pattern_part_end == nullptr ? 0 : 1);
123 }
124 return true; // all parts of pattern matched
125 }
126
127 StringUtils::CommaSeparatedStringIterator::~CommaSeparatedStringIterator() {
128 FREE_C_HEAP_ARRAY(char, _list);
129 }
130
131 ccstrlist StringUtils::CommaSeparatedStringIterator::canonicalize(ccstrlist option_value) {
132 char* canonicalized_list = NEW_C_HEAP_ARRAY(char, strlen(option_value) + 1, mtCompiler);
133 int i = 0;
134 char current;
135 while ((current = option_value[i]) != '\0') {
136 if (current == '\n' || current == ' ') {
137 canonicalized_list[i] = ',';
138 } else {
139 canonicalized_list[i] = current;
140 }
141 i++;
142 }
143 canonicalized_list[i] = '\0';
144 return canonicalized_list;
145 }