1 /*
  2  * Copyright (c) 2014, 2023, 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 "precompiled.hpp"
 26 #include "jvm_io.h"
 27 #include "utilities/debug.hpp"
 28 #include "utilities/stringUtils.hpp"
 29 
 30 #include <ctype.h>
 31 #include <string.h>
 32 
 33 int StringUtils::replace_no_expand(char* string, const char* from, const char* to) {
 34   int replace_count = 0;
 35   size_t from_len = strlen(from);
 36   size_t to_len = strlen(to);
 37   assert(from_len >= to_len, "must not expand input");
 38 
 39   for (char* dst = string; *dst && (dst = strstr(dst, from)) != nullptr;) {
 40     char* left_over = dst + from_len;
 41     memmove(dst, to, to_len);                       // does not copy trailing 0 of <to>
 42     dst += to_len;                                  // skip over the replacement.
 43     memmove(dst, left_over, strlen(left_over) + 1); // copies the trailing 0 of <left_over>
 44     ++ replace_count;
 45   }
 46 
 47   return replace_count;
 48 }
 49 
 50 double StringUtils::similarity(const char* str1, size_t len1, const char* str2, size_t len2) {
 51   assert(str1 != nullptr && str2 != nullptr, "sanity");
 52 
 53   // filter out zero-length strings else we will underflow on len-1 below
 54   if (len1 == 0 || len2 == 0) {
 55     return 0.0;
 56   }
 57 
 58   size_t total = len1 + len2;
 59   size_t hit = 0;
 60 
 61   for (size_t i = 0; i < len1 - 1; i++) {
 62     for (size_t j = 0; j < len2 - 1; j++) {
 63       if ((str1[i] == str2[j]) && (str1[i+1] == str2[j+1])) {
 64         ++hit;
 65         break;
 66       }
 67     }
 68   }
 69 
 70   return 2.0 * (double) hit / (double) total;
 71 }
 72 
 73 const char* StringUtils::strstr_nocase(const char* haystack, const char* needle) {
 74   if (needle[0] == '\0') {
 75     return haystack; // empty needle matches with anything
 76   }
 77   for (size_t i = 0; haystack[i] != '\0'; i++) {
 78     bool matches = true;
 79     for (size_t j = 0; needle[j] != '\0'; j++) {
 80       if (haystack[i + j] == '\0') {
 81         return nullptr; // hit end of haystack, abort
 82       }
 83       if (tolower(haystack[i + j]) != tolower(needle[j])) {
 84         matches = false;
 85         break; // abort, try next i
 86       }
 87     }
 88     if (matches) {
 89       return &haystack[i]; // all j were ok for this i
 90     }
 91   }
 92   return nullptr; // no i was a match
 93 }
 94 
 95 bool StringUtils::is_star_match(const char* star_pattern, const char* str) {
 96   const int N = 1000;
 97   char pattern[N]; // copy pattern into this to ensure null termination
 98   jio_snprintf(pattern, N, "%s", star_pattern);// ensures null termination
 99   char buf[N]; // copy parts of pattern into this
100   const char* str_idx = str;
101   const char* pattern_idx = pattern;
102   while (strlen(pattern_idx) > 0) {
103     // find next section in pattern
104     const char* pattern_part_end = strstr(pattern_idx, "*");
105     const char* pattern_part = pattern_idx;
106     if (pattern_part_end != nullptr) { // copy part into buffer
107       size_t pattern_part_len = pattern_part_end-pattern_part;
108       strncpy(buf, pattern_part, pattern_part_len);
109       buf[pattern_part_len] = '\0'; // end of string
110       pattern_part = buf;
111     }
112     // find this section in s, case insensitive
113     const char* str_match = strstr_nocase(str_idx, pattern_part);
114     if (str_match == nullptr) {
115       return false; // r_part did not match - abort
116     }
117     size_t match_len = strlen(pattern_part);
118     // advance to match position plus part length
119     str_idx = str_match + match_len;
120     // advance by part length and "*"
121     pattern_idx += match_len + (pattern_part_end == nullptr ? 0 : 1);
122   }
123   return true; // all parts of pattern matched
124 }