Example #1
0
    public Value matchPositions(Value str) {
      PatternMatcher matcher = getMatcher();

      // Do the matching
      PatternMatcherInput jStr = new PatternMatcherInput(string(str));
      Pair result = null;
      Pair prev = null;
      boolean found = false;

      while (matcher.contains(jStr, pattern)) {
        found = true;

        MatchResult matchResult = matcher.getMatch();

        for (int i = 0, length = matchResult.groups(); i < length; i++) {
          Pair m =
              new Pair(
                  Quantity.valueOf(matchResult.beginOffset(i)),
                  Quantity.valueOf(matchResult.endOffset(i)));
          Pair elem = new Pair(m, EMPTYLIST);
          if (result == null) result = prev = elem;
          else {
            prev.setCdr(elem);
            prev = elem;
          }
        }
      }

      if (!found) return FALSE;
      else return result;
    }
Example #2
0
  public RubyValue pre_match() {
    String res = "";
    int nMatchEnd = result_.beginOffset(0);
    if (nMatchEnd >= 0 && nMatchEnd < str_.length()) res = str_.substring(0, nMatchEnd);

    if (res == null) return RubyConstant.QNIL;

    return ObjectFactory.createString(res);
  }
  // Overridden Methods
  @Override
  protected void inspectContents(File file, String contents) {
    StringBuffer buf = new StringBuffer();

    // Split it into lines
    StringSplitter splitter = new StringSplitter(contents, getLineEnding());

    // Scan each line
    int totalMatchesForThisFile = 0;
    int lineCount = 1;
    while (splitter.hasMoreElements()) {
      String line = (String) splitter.nextElement();

      input = new PatternMatcherInput(line);
      try {
        while (util.match(regex, input)) {
          result = util.getMatch();
          totalMatchesForThisFile++;
          totalNumberOfMatches++;
          buf.append(
              "  line: " + lineCount + " position: " + result.beginOffset(0) + getLineEnding());
        }
      } catch (MalformedPerl5PatternException e) {
        System.out.println("MalformedPerl5PatternException: " + e.getMessage());
        System.out.println("Valid expression: [m]/pattern/[i][m][s][x]");
        return;
      }

      lineCount++;
    }

    // Output the results
    if (totalMatchesForThisFile > 0) {
      if (totalMatchesForThisFile == 1) {
        System.out.println(
            "Found " + totalMatchesForThisFile + " match in file: " + file.getPath());
      } else {
        System.out.println(
            "Found " + totalMatchesForThisFile + " matches in file: " + file.getPath());
      }
      System.out.print(buf.toString());
      System.out.println("");
    }
  }
Example #4
0
  private void initTemplate() {
    if (template != null) {
      return;
    }
    // Contains Strings and Integers
    List<Object> combined = new ArrayList<Object>();
    String rawTemplate = getTemplate();
    PatternMatcher matcher = JMeterUtils.getMatcher();
    Pattern templatePattern =
        JMeterUtils.getPatternCache()
            .getPattern(
                "\\$(\\d+)\\$" // $NON-NLS-1$
                ,
                Perl5Compiler.READ_ONLY_MASK & Perl5Compiler.SINGLELINE_MASK);
    if (log.isDebugEnabled()) {
      log.debug("Pattern = " + templatePattern.getPattern());
      log.debug("template = " + rawTemplate);
    }
    int beginOffset = 0;
    MatchResult currentResult;
    PatternMatcherInput pinput = new PatternMatcherInput(rawTemplate);
    while (matcher.contains(pinput, templatePattern)) {
      currentResult = matcher.getMatch();
      final int beginMatch = currentResult.beginOffset(0);
      if (beginMatch > beginOffset) { // string is not empty
        combined.add(rawTemplate.substring(beginOffset, beginMatch));
      }
      combined.add(Integer.valueOf(currentResult.group(1))); // add match as Integer
      beginOffset = currentResult.endOffset(0);
    }

    if (beginOffset < rawTemplate.length()) { // trailing string is not empty
      combined.add(rawTemplate.substring(beginOffset, rawTemplate.length()));
    }
    if (log.isDebugEnabled()) {
      log.debug("Template item count: " + combined.size());
      for (Object o : combined) {
        log.debug(o.getClass().getSimpleName() + " '" + o.toString() + "'");
      }
    }
    template = combined;
  }