Skip to content

Instantly share code, notes, and snippets.

@juanfal
Last active November 22, 2023 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanfal/73ec7f846ec2500fe1c4301c46fbfad1 to your computer and use it in GitHub Desktop.
Save juanfal/73ec7f846ec2500fe1c4301c46fbfad1 to your computer and use it in GitHub Desktop.
string with wild chars
// t11e17.stringglobsearch.cpp
// juanfc 2023-11-22
// https://gist.github.com/73ec7f846ec2500fe1c4301c46fbfad1
#include <iostream>
using namespace std;
int glob(string s, string ss, int from=0);
int main()
{
cout << glob("ejer01.cpp", "ejer??.cpp") << endl;
cout << glob("ejer01.cpp", ".cpp") << endl;
return 0;
}
int glob(string s, string ss, int from)
{
int i = from, j = 0;
int imax = s.size();
int sslen = ss.size();
while (i <= imax and j < sslen) {
if (ss[j] == '?' or s[i+j] == ss[j]) {
++j;
} else {
++i;
j = 0;
}
}
return (j==sslen)? i : -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment