| Home Page | Recent Changes | Preferences

MaskedCompare

With the algoritm below you can compare a string with a wild card string.

There are two types of wild cards: * to match zero or more characters and ? to match a single character

// Internal function used for MaskedCompare
static private final function bool _match(out string mask, out string target)
{
  local string m, mp, cp;
  m = Left(mask, 1);
  while ((target != "") && (m != "*"))
  {
    if ((m != Left(target, 1)) && (m != "?")) return false;
    mask = Mid(Mask, 1);
    target = Mid(target, 1);
        m = Left(mask, 1);
  }

  while (target != "") 
  {
        if (m == "*") 
    {
      mask = Mid(Mask, 1);
            if (mask == "") return true; // only "*" mask -> always true
            mp = mask;
            cp = Mid(target, 1);
      m = Left(mask, 1);
        } 
    else if ((m == Left(target, 1)) || (m == "?")) 
    {
            mask = Mid(Mask, 1);
      target = Mid(target, 1);
        m = Left(mask, 1);
        } 
    else 
    {
            mask = mp;
      m = Left(mask, 1);
            target = cp;
      cp = Mid(cp, 1);
        }
    }

  while (Left(mask, 1) == "*") 
  {
        mask = Mid(Mask, 1);
    }
    return (mask == "");
}

// Compare a string with a mask
// Wildcards: * = X chars; ? = 1 char
// Wildcards can appear anywhere in the mask
static final function bool MaskedCompare(coerce string target, string mask, optional bool casesensitive)
{
  if (!casesensitive)
  {
    mask = Caps(mask);
    target = Caps(target);
  }
  if (mask == "*") return true;

  return _match(mask, target);
}

The Unreal Engine Documentation Site

Wiki Community

Topic Categories

Image Uploads

Random Page

Recent Changes

Offline Wiki

Unreal Engine

Console Commands

Terminology

Mapping Topics

Mapping Lessons

UnrealEd Interface

Questions&Answers

Scripting Topics

Scripting Lessons

Making Mods

Class Tree

Questions&Answers

Modeling Topics

Questions&Answers

Log In