1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-11 04:32:26 +00:00
plantuml/src/net/sourceforge/plantuml/command/regex/RegexConcat.java

171 lines
4.9 KiB
Java
Raw Normal View History

2010-11-15 20:35:36 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2022-03-07 19:33:46 +00:00
* (C) Copyright 2009-2023, Arnaud Roques
2010-11-15 20:35:36 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://plantuml.com
2010-11-15 20:35:36 +00:00
*
2017-03-15 19:13:31 +00:00
* If you like this project or if you find it useful, you can support us at:
*
* http://plantuml.com/patreon (only 1$ per month!)
* http://plantuml.com/paypal
*
2010-11-15 20:35:36 +00:00
* This file is part of PlantUML.
*
* PlantUML is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PlantUML distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
2013-12-10 19:36:50 +00:00
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
2010-11-15 20:35:36 +00:00
* License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
*
* Original Author: Arnaud Roques
*
*
*/
package net.sourceforge.plantuml.command.regex;
2019-06-26 19:24:49 +00:00
import java.util.concurrent.atomic.AtomicLong;
import net.sourceforge.plantuml.StringLocated;
2010-11-15 20:35:36 +00:00
2019-09-14 18:12:04 +00:00
public final class RegexConcat extends RegexComposed implements IRegex {
2010-11-15 20:35:36 +00:00
2019-06-26 19:24:49 +00:00
private final AtomicLong foxRegex = new AtomicLong(-1L);
2021-08-30 17:13:54 +00:00
private int limitSize;
2010-11-15 20:35:36 +00:00
2021-05-14 08:42:57 +00:00
// private static final Set<String> PRINTED2 = new HashSet<>();
2019-06-26 19:24:49 +00:00
public static void printCacheInfo() {
2022-12-16 16:20:14 +00:00
// if (OptionFlags.getInstance().isVerbose())
// synchronized (cache) {
//
// final NumberFormat nf = NumberFormat.getInstance(Locale.US);
//
// int nbCompiled = 0;
// int nbInvoked = 0;
// for (RegexConcat reg : cache.values()) {
// if (reg.isCompiled())
// nbCompiled++;
//
// if (reg.invoked())
// nbInvoked++;
//
// }
// Log.info("Regex total/invoked/compiled " + nf.format(cache.size()) + "/" + nf.format(nbInvoked) + "/"
// + nf.format(nbCompiled));
// Log.info("Matches escaped " + nf.format(vescaped.get()) + "/" + nf.format(vtot.get()));
// Log.info("Matches created " + nf.format(nbCreateMatches.get()));
// }
2019-06-26 19:24:49 +00:00
}
public RegexConcat(IRegex... partials) {
super(partials);
}
private long foxRegex() {
2022-12-16 16:20:14 +00:00
final long result = foxRegex.get();
if (result == -1L) {
2019-06-26 19:24:49 +00:00
long tmp = 0L;
2019-09-14 18:12:04 +00:00
for (int i = 1; i < partials().size() - 1; i++) {
final IRegex part = partials().get(i);
2019-06-26 19:24:49 +00:00
if (part instanceof RegexLeaf) {
final RegexLeaf leaf = (RegexLeaf) part;
tmp = tmp | leaf.getFoxSignature();
}
}
foxRegex.set(tmp);
2022-12-16 16:20:14 +00:00
return tmp;
2010-11-15 20:35:36 +00:00
}
2022-12-16 16:20:14 +00:00
return result;
2019-06-26 19:24:49 +00:00
}
public static RegexConcat build(String key, IRegex... partials) {
2022-12-16 16:20:14 +00:00
return buildInternal(partials);
// RegexConcat result = cache.get(key);
// if (result == null) {
// cache.putIfAbsent(key, buildInternal(partials));
// result = cache.get(key);
// // System.err.println("cache size=" + cache.size());
// // } else {
// // synchronized (PRINTED2) {
// // if (PRINTED2.contains(key) == false) {
// // System.err.println("if (key.equals(\"" + key + "\")) return
// // buildInternal(partials);");
// // }
// // PRINTED2.add(key);
// }
// return result;
2019-06-26 19:24:49 +00:00
}
private static RegexConcat buildInternal(IRegex... partials) {
final RegexConcat result = new RegexConcat(partials);
assert partials[0] == RegexLeaf.start();
assert partials[partials.length - 1] == RegexLeaf.end();
return result;
2010-11-15 20:35:36 +00:00
}
2015-04-07 18:18:37 +00:00
2019-09-14 18:12:04 +00:00
private boolean invoked() {
return foxRegex.get() != -1L;
}
2022-12-16 16:20:14 +00:00
// static private final Set<String> PRINTED = new HashSet<>();
// static private final Set<String> ZERO = new HashSet<>();
2010-11-15 20:35:36 +00:00
@Override
2019-06-26 19:24:49 +00:00
public boolean match(StringLocated s) {
2022-10-20 16:58:12 +00:00
if (limitSize != 0 && s.getString().length() > limitSize)
2021-08-30 17:13:54 +00:00
return false;
2022-10-20 16:58:12 +00:00
2022-12-16 16:20:14 +00:00
// vtot.incrementAndGet();
2019-06-26 19:24:49 +00:00
final long foxRegex = foxRegex();
2022-12-16 16:20:14 +00:00
// synchronized (PRINTED) {
// final String full = getFullSlow();
// final boolean added = PRINTED.add(full);
// if (added && foxRegex == 0L) {
// ZERO.add(full);
// System.err.println("PR " + ZERO.size() + "/" + PRINTED.size() + " "
// + FoxSignature.backToString(foxRegex) + " " + full);
// }
// }
2019-06-26 19:24:49 +00:00
if (foxRegex != 0L) {
final long foxLine = s.getFoxSignature();
final long check = foxRegex & foxLine;
2021-08-30 17:13:54 +00:00
// System.err.println("r=" + getFullSlow() + " s=" + s + " line=" + foxLine + "
// regex" + foxRegex + " "
2019-06-26 19:24:49 +00:00
// + check + " <" + FoxSignature.backToString(check) + ">");
2022-12-16 16:20:14 +00:00
if (check != foxRegex) {
// vescaped.incrementAndGet();
2019-06-26 19:24:49 +00:00
return false;
2022-12-16 16:20:14 +00:00
}
2019-06-26 19:24:49 +00:00
}
return super.match(s);
}
@Override
protected String getFullSlow() {
final StringBuilder sb = new StringBuilder();
2022-10-20 16:58:12 +00:00
for (IRegex p : partials())
2019-06-26 19:24:49 +00:00
sb.append(p.getPattern());
2022-10-20 16:58:12 +00:00
2019-06-26 19:24:49 +00:00
return sb.toString();
2010-11-15 20:35:36 +00:00
}
2021-08-30 17:13:54 +00:00
public RegexConcat protectSize(int size) {
limitSize = size;
return this;
}
2010-11-15 20:35:36 +00:00
}