001package org.unix4j.unix.sed;
002
003import java.util.regex.Pattern;
004
005import org.unix4j.processor.LineProcessor;
006import org.unix4j.util.StringUtil;
007
008abstract class AbstractRegexpProcessor extends AbstractSedProcessor {
009        
010        protected final Pattern regexp;
011
012        public AbstractRegexpProcessor(Command command, SedArguments args, LineProcessor output) {
013                super(command, args, output);
014                if (args.isIgnoreCase()) {
015                        this.regexp = Pattern.compile(getRegexp(args), Pattern.CASE_INSENSITIVE);
016                } else {
017                        this.regexp = Pattern.compile(getRegexp(args));
018                }
019        }
020
021        protected static SedArguments parsePatternFlags(Command command, SedArguments args, String script, int start) {
022                final int end = StringUtil.findWhitespace(script, start);
023                for (int i = start; i < end; i++) {
024                        final char flag = script.charAt(i);
025                        if (flag == 'I' && !args.isIgnoreCase()) {
026                                final SedOptions.Default options = new SedOptions.Default(args.getOptions());
027                                options.set(SedOption.ignoreCase);
028                                args = new SedArguments(options);
029                        } else if (flag == command.commandChar) {
030                                //ignore
031                        } else {
032                                throw new IllegalArgumentException("invalid pattern flags in sed script: " + script);
033                        }
034                }
035                return args;
036        }
037}