comparison src/org/tmatesoft/hg/internal/EncodeDirPathHelper.java @ 616:5e0313485eef

encode directories as demanded by fncache format
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 14 May 2013 17:31:35 +0200
parents
children
comparison
equal deleted inserted replaced
615:84104448a0bf 616:5e0313485eef
1 /*
2 * Copyright (c) 2013 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.internal;
18
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21
22 import org.tmatesoft.hg.util.PathRewrite;
23
24 /**
25 * <blockquote cite="http://mercurial.selenic.com/wiki/FileFormats#data.2F">Directory names ending in .i or .d have .hg appended</blockquote>
26 *
27 * @see http://mercurial.selenic.com/wiki/FileFormats#data.2F
28 * @author Artem Tikhomirov
29 * @author TMate Software Ltd.
30 */
31 final class EncodeDirPathHelper implements PathRewrite {
32 private final Pattern suffix2replace;
33
34 public EncodeDirPathHelper() {
35 suffix2replace = Pattern.compile("\\.([id]|hg)/");
36 }
37
38 public CharSequence rewrite(CharSequence p) {
39 Matcher suffixMatcher = suffix2replace.matcher(p);
40 CharSequence path;
41 // Matcher.replaceAll, but without extra toString
42 boolean found = suffixMatcher.find();
43 if (found) {
44 StringBuffer sb = new StringBuffer(p.length() + 20);
45 do {
46 suffixMatcher.appendReplacement(sb, ".$1.hg/");
47 } while (found = suffixMatcher.find());
48 suffixMatcher.appendTail(sb);
49 path = sb;
50 } else {
51 path = p;
52 }
53 return path;
54 }
55
56 }