Skip to content

Latest commit

 

History

History
352 lines (292 loc) · 14.5 KB

188.md

File metadata and controls

352 lines (292 loc) · 14.5 KB
Info

Example

static_assert(not ctre::match<"hello">("world"));

static_assert(ctre::match<"hello world">("hello world"));
static_assert(ctre::match<".*">("hello world"));

https://gcc.godbolt.org/z/dKKTo5

Puzzle

  • Can you fill TODO with appropriate regular expressions?
int main() {
  using namespace boost::ut;

  "regex"_test = [] {
    should("match numbers") = [] {
       constexpr auto number = ctll::fixed_string{""}; // TODO

       expect(not ctre::match<number>("")) << "empty";
       expect(not ctre::match<number>("O")) << "letter";
       expect(not ctre::match<number>("abc")) << "string";
       expect(not ctre::match<number>("1.a")) << "partial number";
       expect(not ctre::match<number>("1.2.3")) << "multiple dots";
       expect(not ctre::match<number>("1 2")) << "spaces";
       expect(not ctre::match<number>("--42")) << "double minus";
       expect(not ctre::match<number>("42-")) << "minus at the end";

       expect(ctre::match<number>("1")) << "single";
       expect(ctre::match<number>("9")) << "single";
       expect(ctre::match<number>("1234")) << "multipv4le";
       expect(ctre::match<number>("-42")) << "negative";
       expect(ctre::match<number>("1.234")) << "floating-point";
    };

    should("match an IPv4 address") = [] {
      constexpr auto ipv4 = ctll::fixed_string{""}; // TODO

      expect(not ctre::match<ipv4>("")) << "empty";
      expect(not ctre::match<ipv4>("1 2 3 4")) << "spaces instead of spaces";
      expect(not ctre::match<ipv4>(".1.2.3.4.")) << "too many dots";
      expect(not ctre::match<ipv4>("1.2.3")) << "too litle dots";
      expect(not ctre::match<ipv4>("1.2.3.")) << "missing number";
      expect(not ctre::match<ipv4>("a.b.c.d")) << "letters instead of numbers";
      expect(not ctre::match<ipv4>("a.b.c.d")) << "letters instead of numbers";

      expect(ctre::match<ipv4>("1.2.3.4")) << "simple";
      expect(ctre::match<ipv4>("1.2.3.4")) << "simple";
      expect(ctre::match<ipv4>("10.0.0.0")) << "A";
      expect(ctre::match<ipv4>("172.16.0.1")) << "B";
      expect(ctre::match<ipv4>("192.168.0.2")) << "C";
      expect(ctre::match<ipv4>("127.0.0.1")) << "loopback";
      expect(ctre::match<ipv4>("224.0.0.1")) << "multicast";
      expect(ctre::match<ipv4>("255.255.255.255")) << "broadcast";
    };
  };
}

https://gcc.godbolt.org/z/KhPaEo

Solutions

"regex"_test = [] {
  should("match numbers") = [] {
     constexpr auto number = ctll::fixed_string{R"(^-?\d+(\.\d+)?$)"};

     expect(not ctre::match<number>("")) << "empty";
     expect(not ctre::match<number>("O")) << "letter";
     expect(not ctre::match<number>("abc")) << "string";
     expect(not ctre::match<number>("1.a")) << "partial number";
     expect(not ctre::match<number>("1.2.3")) << "multiple dots";
     expect(not ctre::match<number>("1 2")) << "spaces";
     expect(not ctre::match<number>("--42")) << "double minus";
     expect(not ctre::match<number>("42-")) << "minus at the end";

     expect(ctre::match<number>("1")) << "single";
     expect(ctre::match<number>("9")) << "single";
     expect(ctre::match<number>("1234")) << "multiple";
     expect(ctre::match<number>("-42")) << "negative";
     expect(ctre::match<number>("1.234")) << "floating-point";
  };

  should("match an IPv4 address") = [] {
    constexpr auto ipv4 = ctll::fixed_string{R"(^(\d{1,3}\.){3}\d{1,3}$)"};

    expect(not ctre::match<ipv4>("")) << "empty";
    expect(not ctre::match<ipv4>("1 2 3 4")) << "spaces instead of spaces";
    expect(not ctre::match<ipv4>(".1.2.3.4.")) << "too many dots";
    expect(not ctre::match<ipv4>("1.2.3")) << "too litle dots";
    expect(not ctre::match<ipv4>("1.2.3.")) << "missing number";
    expect(not ctre::match<ipv4>("a.b.c.d")) << "letters instead of numbers";
    expect(not ctre::match<ipv4>("a.b.c.d")) << "letters instead of numbers";

    expect(ctre::match<ipv4>("1.2.3.4")) << "simple";
    expect(ctre::match<ipv4>("1.2.3.4")) << "simple";
    expect(ctre::match<ipv4>("10.0.0.0")) << "A";
    expect(ctre::match<ipv4>("172.16.0.1")) << "B";
    expect(ctre::match<ipv4>("192.168.0.2")) << "C";
    expect(ctre::match<ipv4>("127.0.0.1")) << "loopback";
    expect(ctre::match<ipv4>("224.0.0.1")) << "multicast";
    expect(ctre::match<ipv4>("255.255.255.255")) << "broadcast";
  };
};

https://gcc.godbolt.org/z/aevP8M

"regex"_test = [] {
  should("match numbers") = [] {
     constexpr auto number = ctll::fixed_string{"-?\\d+(\\.\\d+)?"}; // TODO

     expect(not ctre::match<number>("")) << "empty";
     expect(not ctre::match<number>("O")) << "letter";
     expect(not ctre::match<number>("abc")) << "string";
     expect(not ctre::match<number>("1.a")) << "partial number";
     expect(not ctre::match<number>("1.2.3")) << "multiple dots";
     expect(not ctre::match<number>("1 2")) << "spaces";
     expect(not ctre::match<number>("--42")) << "double minus";
     expect(not ctre::match<number>("42-")) << "minus at the end";

     expect(ctre::match<number>("1")) << "single";
     expect(ctre::match<number>("9")) << "single";
     expect(ctre::match<number>("1234")) << "multipv4le";
     expect(ctre::match<number>("-42")) << "negative";
     expect(ctre::match<number>("1.234")) << "floating-point";
  };

  should("match an IPv4 address") = [] {
    constexpr auto ipv4 = ctll::fixed_string{"\\d+\\.\\d+\\.\\d+\\.\\d+"}; // TODO

    expect(not ctre::match<ipv4>("")) << "empty";
    expect(not ctre::match<ipv4>("1 2 3 4")) << "spaces instead of spaces";
    expect(not ctre::match<ipv4>(".1.2.3.4.")) << "too many dots";
    expect(not ctre::match<ipv4>("1.2.3")) << "too litle dots";
    expect(not ctre::match<ipv4>("1.2.3.")) << "missing number";
    expect(not ctre::match<ipv4>("a.b.c.d")) << "letters instead of numbers";
    expect(not ctre::match<ipv4>("a.b.c.d")) << "letters instead of numbers";

    expect(ctre::match<ipv4>("1.2.3.4")) << "simple";
    expect(ctre::match<ipv4>("1.2.3.4")) << "simple";
    expect(ctre::match<ipv4>("10.0.0.0")) << "A";
    expect(ctre::match<ipv4>("172.16.0.1")) << "B";
    expect(ctre::match<ipv4>("192.168.0.2")) << "C";
    expect(ctre::match<ipv4>("127.0.0.1")) << "loopback";
    expect(ctre::match<ipv4>("224.0.0.1")) << "multicast";
    expect(ctre::match<ipv4>("255.255.255.255")) << "broadcast";
  };
};

https://gcc.godbolt.org/z/3E1W93

"regex"_test = [] {
  should("match numbers") = [] {
     constexpr auto number = ctll::fixed_string{R"(^-?\d+(\.\d+)?$)"};

     expect(not ctre::match<number>("")) << "empty";
     expect(not ctre::match<number>("O")) << "letter";
     expect(not ctre::match<number>("abc")) << "string";
     expect(not ctre::match<number>("1.a")) << "partial number";
     expect(not ctre::match<number>("1.2.3")) << "multiple dots";
     expect(not ctre::match<number>("1 2")) << "spaces";
     expect(not ctre::match<number>("--42")) << "double minus";
     expect(not ctre::match<number>("42-")) << "minus at the end";

     expect(ctre::match<number>("1")) << "single";
     expect(ctre::match<number>("9")) << "single";
     expect(ctre::match<number>("1234")) << "multipv4le";
     expect(ctre::match<number>("-42")) << "negative";
     expect(ctre::match<number>("1.234")) << "floating-point";
  };

  should("match an IPv4 address") = [] {
    constexpr auto ipv4 = ctll::fixed_string{R"(^\d+(\.\d+){3}$)"};

    expect(not ctre::match<ipv4>("")) << "empty";
    expect(not ctre::match<ipv4>("1 2 3 4")) << "spaces instead of spaces";
    expect(not ctre::match<ipv4>(".1.2.3.4.")) << "too many dots";
    expect(not ctre::match<ipv4>("1.2.3")) << "too litle dots";
    expect(not ctre::match<ipv4>("1.2.3.")) << "missing number";
    expect(not ctre::match<ipv4>("a.b.c.d")) << "letters instead of numbers";
    expect(not ctre::match<ipv4>("a.b.c.d")) << "letters instead of numbers";

    expect(ctre::match<ipv4>("1.2.3.4")) << "simple";
    expect(ctre::match<ipv4>("1.2.3.4")) << "simple";
    expect(ctre::match<ipv4>("10.0.0.0")) << "A";
    expect(ctre::match<ipv4>("172.16.0.1")) << "B";
    expect(ctre::match<ipv4>("192.168.0.2")) << "C";
    expect(ctre::match<ipv4>("127.0.0.1")) << "loopback";
    expect(ctre::match<ipv4>("224.0.0.1")) << "multicast";
    expect(ctre::match<ipv4>("255.255.255.255")) << "broadcast";
  };
};

https://gcc.godbolt.org/z/h785Ka

  "regex"_test = [] {
    should("match numbers") = [] {
       constexpr auto number = ctll::fixed_string{R"(-?\d+(\.\d+)?)"};

       expect(not ctre::match<number>("")) << "empty";
       expect(not ctre::match<number>("O")) << "letter";
       expect(not ctre::match<number>("abc")) << "string";
       expect(not ctre::match<number>("1.a")) << "partial number";
       expect(not ctre::match<number>("1.2.3")) << "multiple dots";
       expect(not ctre::match<number>("1 2")) << "spaces";
       expect(not ctre::match<number>("--42")) << "double minus";
       expect(not ctre::match<number>("42-")) << "minus at the end";

       expect(ctre::match<number>("1")) << "single";
       expect(ctre::match<number>("9")) << "single";
       expect(ctre::match<number>("1234")) << "multipv4le";
       expect(ctre::match<number>("-42")) << "negative";
       expect(ctre::match<number>("1.234")) << "floating-point";
    };

    should("match an IPv4 address") = [] {
      constexpr auto ipv4 = ctll::fixed_string{R"(^\d{1,3}(\.\d{1,3}){3})"};

      expect(not ctre::match<ipv4>("")) << "empty";
      expect(not ctre::match<ipv4>("1 2 3 4")) << "spaces instead of spaces";
      expect(not ctre::match<ipv4>(".1.2.3.4.")) << "too many dots";
      expect(not ctre::match<ipv4>("1.2.3")) << "too litle dots";
      expect(not ctre::match<ipv4>("1.2.3.")) << "missing number";
      expect(not ctre::match<ipv4>("a.b.c.d")) << "letters instead of numbers";
      expect(not ctre::match<ipv4>("a.b.c.d")) << "letters instead of numbers";

      expect(ctre::match<ipv4>("1.2.3.4")) << "simple";
      expect(ctre::match<ipv4>("1.2.3.4")) << "simple";
      expect(ctre::match<ipv4>("10.0.0.0")) << "A";
      expect(ctre::match<ipv4>("172.16.0.1")) << "B";
      expect(ctre::match<ipv4>("192.168.0.2")) << "C";
      expect(ctre::match<ipv4>("127.0.0.1")) << "loopback";
      expect(ctre::match<ipv4>("224.0.0.1")) << "multicast";
      expect(ctre::match<ipv4>("255.255.255.255")) << "broadcast";
    };
  };

https://gcc.godbolt.org/z/7Yfo4v

"regex"_test = [] {
  should("match numbers") = [] {
     constexpr auto number = ctll::fixed_string{R"(-?\d+(\.\d+)?)"};

     expect(not ctre::match<number>("")) << "empty";
     expect(not ctre::match<number>("O")) << "letter";
     expect(not ctre::match<number>("abc")) << "string";
     expect(not ctre::match<number>("1.a")) << "partial number";
     expect(not ctre::match<number>("1.2.3")) << "multiple dots";
     expect(not ctre::match<number>("1 2")) << "spaces";
     expect(not ctre::match<number>("--42")) << "double minus";
     expect(not ctre::match<number>("42-")) << "minus at the end";

     expect(ctre::match<number>("1")) << "single";
     expect(ctre::match<number>("9")) << "single";
     expect(ctre::match<number>("1234")) << "multipv4le";
     expect(ctre::match<number>("-42")) << "negative";
     expect(ctre::match<number>("1.234")) << "floating-point";
  };

  should("match an IPv4 address") = [] {
    constexpr auto ipv4 = ctll::fixed_string{R"((\d{1,3}\.){3}\d{1,3})"};

    expect(not ctre::match<ipv4>("")) << "empty";
    expect(not ctre::match<ipv4>("1 2 3 4")) << "spaces instead of spaces";
    expect(not ctre::match<ipv4>(".1.2.3.4.")) << "too many dots";
    expect(not ctre::match<ipv4>("1.2.3")) << "too litle dots";
    expect(not ctre::match<ipv4>("1.2.3.")) << "missing number";
    expect(not ctre::match<ipv4>("a.b.c.d")) << "letters instead of numbers";
    expect(not ctre::match<ipv4>("a.b.c.d")) << "letters instead of numbers";

    expect(ctre::match<ipv4>("1.2.3.4")) << "simple";
    expect(ctre::match<ipv4>("1.2.3.4")) << "simple";
    expect(ctre::match<ipv4>("10.0.0.0")) << "A";
    expect(ctre::match<ipv4>("172.16.0.1")) << "B";
    expect(ctre::match<ipv4>("192.168.0.2")) << "C";
    expect(ctre::match<ipv4>("127.0.0.1")) << "loopback";
    expect(ctre::match<ipv4>("224.0.0.1")) << "multicast";
    expect(ctre::match<ipv4>("255.255.255.255")) << "broadcast";
  };
};

https://gcc.godbolt.org/z/cvEMzG

"regex"_test = [] {
  should("match numbers") = [] {
     constexpr auto number = ctll::fixed_string{"-?\\d+\\.?\\d*"}; // TODO

     expect(not ctre::match<number>("")) << "empty";
     expect(not ctre::match<number>("O")) << "letter";
     expect(not ctre::match<number>("abc")) << "string";
     expect(not ctre::match<number>("1.a")) << "partial number";
     expect(not ctre::match<number>("1.2.3")) << "multiple dots";
     expect(not ctre::match<number>("1 2")) << "spaces";
     expect(not ctre::match<number>("--42")) << "double minus";
     expect(not ctre::match<number>("42-")) << "minus at the end";

     expect(ctre::match<number>("1")) << "single";
     expect(ctre::match<number>("9")) << "single";
     expect(ctre::match<number>("1234")) << "multipv4le";
     expect(ctre::match<number>("-42")) << "negative";
     expect(ctre::match<number>("1.234")) << "floating-point";
  };

  should("match an IPv4 address") = [] {
    constexpr auto ipv4 = ctll::fixed_string{"(?:[0-9]{1,3}\\.){3}[0-9]{1,3}"}; // TODO

    expect(not ctre::match<ipv4>("")) << "empty";
    expect(not ctre::match<ipv4>("1 2 3 4")) << "spaces instead of spaces";
    expect(not ctre::match<ipv4>(".1.2.3.4.")) << "too many dots";
    expect(not ctre::match<ipv4>("1.2.3")) << "too litle dots";
    expect(not ctre::match<ipv4>("1.2.3.")) << "missing number";
    expect(not ctre::match<ipv4>("a.b.c.d")) << "letters instead of numbers";
    expect(not ctre::match<ipv4>("a.b.c.d")) << "letters instead of numbers";

    expect(ctre::match<ipv4>("1.2.3.4")) << "simple";
    expect(ctre::match<ipv4>("1.2.3.4")) << "simple";
    expect(ctre::match<ipv4>("10.0.0.0")) << "A";
    expect(ctre::match<ipv4>("172.16.0.1")) << "B";
    expect(ctre::match<ipv4>("192.168.0.2")) << "C";
    expect(ctre::match<ipv4>("127.0.0.1")) << "loopback";
    expect(ctre::match<ipv4>("224.0.0.1")) << "multicast";
    expect(ctre::match<ipv4>("255.255.255.255")) << "broadcast";
  };
};

https://gcc.godbolt.org/z/EYWcfs