1 /*
  2  * Copyright (c) 2024, 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.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 package hat.util;
 26 
 27 import java.util.function.BiFunction;
 28 import java.util.function.Predicate;
 29 import java.util.regex.Matcher;
 30 import java.util.regex.Pattern;
 31 
 32 public record Regex(Pattern pattern) {
 33     public interface Match {
 34         boolean matched();
 35     }
 36 
 37     public interface OK extends Match {
 38         Regex regex();
 39         Matcher matcher();
 40         default  String string(int idx) {
 41             return matcher().group(idx);
 42         }
 43         static float hex2Float(String s) {
 44             return (s.startsWith("-")) ?
 45                     (-Integer.parseInt(s.substring(2), 16) / 64f): (Integer.parseInt(s.substring(1), 16) / 64f);
 46         }
 47         default float f(int idx) {
 48             return hex2Float(string(idx));
 49         }
 50 
 51         default  int i(int idx) {
 52             return Integer.parseInt(string(idx));
 53         }
 54     }
 55 
 56 
 57     public record DefaultOk(Regex regex, Matcher matcher, boolean matched) implements OK {
 58         public static OK of(Regex regex, Matcher matcher) {
 59             return new DefaultOk(regex, matcher, true);
 60         }
 61     }
 62 
 63     record FAIL(boolean matched) implements Match {
 64         public static FAIL of() {
 65             return new FAIL(false);
 66         }
 67     }
 68 
 69     public static Regex of(String... strings) {
 70         return new Regex(Pattern.compile(String.join("", strings)));
 71     }
 72 
 73     public static Match any(String line, Regex... regexes) {
 74         for (Regex r : regexes) {
 75             if (r.is(line) instanceof OK ok) {
 76                 return ok;
 77             }
 78         }
 79         return FAIL.of();
 80     }
 81     public Match is(String s, Predicate<Matcher> matcherPredicate, BiFunction<Regex,Matcher,OK> factory) {
 82         if (pattern.matcher(s) instanceof Matcher matcher && matcher.matches() && matcherPredicate.test(matcher)) {
 83             return factory.apply(this, matcher);
 84         } else {
 85             return FAIL.of();
 86         }
 87     }
 88     public Match is(String s, Predicate<Matcher> matcherPredicate) {
 89         return is(s, matcherPredicate,(r,m)->new DefaultOk(r,m, true));
 90     }
 91     public Match is(String s) {
 92         return is(s, _->true);
 93     }
 94     public boolean matches(String s, Predicate<Matcher> matcherPredicate) {
 95         return is(s, matcherPredicate).matched();
 96     }
 97     public boolean matches(String s) {
 98         return is(s).matched();
 99     }
100     public boolean matchesOrThrow(String s) {
101         if(!is(s).matched()){
102             throw new RuntimeException("failed expected match");
103         }else{
104             return true;
105         }
106     }
107 }