changeset 136:947bf231acbb

Strip off comments in config file
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 16 Feb 2011 22:08:58 +0100
parents 3959bffb14e9
children 144d771ee73c
files TODO design.txt src/org/tmatesoft/hg/internal/ConfigFile.java
diffstat 3 files changed, 7 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/TODO	Wed Feb 16 22:00:39 2011 +0100
+++ b/TODO	Wed Feb 16 22:08:58 2011 +0100
@@ -46,9 +46,6 @@
   - Data access - not bytes, but ByteChannel
   - HgRepository constants (TIP, BAD, WC) to HgRevisions enum
 
-* defects
-  - ConfigFile to strip comments from values (#)
-
 Proposed:
 - LogCommand.revision(int... rev)+ to walk selected revisions only (list->sort(array) on execute, binary search)
 - LogCommand.before(Date date) and .after()
--- a/design.txt	Wed Feb 16 22:00:39 2011 +0100
+++ b/design.txt	Wed Feb 16 22:08:58 2011 +0100
@@ -86,7 +86,7 @@
   + RepositoryTreeWalker to ManifestCommand to match other command classes 
 
 * defects
-  - ConfigFile to strip comments from values (#)
+  + ConfigFile to strip comments from values (#)
 
 <<<<<
 
--- a/src/org/tmatesoft/hg/internal/ConfigFile.java	Wed Feb 16 22:00:39 2011 +0100
+++ b/src/org/tmatesoft/hg/internal/ConfigFile.java	Wed Feb 16 22:08:58 2011 +0100
@@ -107,10 +107,14 @@
 			Map<String,String> section = new LinkedHashMap<String, String>();
 			while ((line = br.readLine()) != null) {
 				line = line.trim();
+				int x;
+				if ((x = line.indexOf('#')) != -1) {
+					// do not keep comments in memory, get new, shorter string
+					line = new String(line.substring(0, x).trim());
+				}
 				if (line.length() <= 2) { // a=b or [a] are at least of length 3
 					continue;
 				}
-				int x;
 				if (line.charAt(0) == '[' && line.charAt(line.length() - 1) == ']') {
 					sectionName = line.substring(1, line.length() - 1);
 					if (sections.indexOf(sectionName) == -1) {
@@ -120,6 +124,7 @@
 						section = null; // drop cached value
 					}
 				} else if ((x = line.indexOf('=')) != -1) {
+					// share char[] of the original string
 					String key = line.substring(0, x).trim();
 					String value = line.substring(x+1).trim();
 					if (section == null) {