@Test public void shouldReturnFileContentWithoutComments() { FakeFile file = new FakeFile("Hello world ", "this is the content ", "of a real file"); String result = removeComments.remove(file); assertEquals("Hello world this is the content of a real file", result); }
public String remove(FakeFile file) { if (file == null) { throw new IllegalArgumentException("You can't pass a null file as argument."); } StringBuilder result = new StringBuilder(); boolean openComment = false; String line = file.getLine(); while (line != null) { char previous = ANY_CHAR; for (char c : line.toCharArray()) { if (openComment) { if (c == SLASH && previous == ASTERISK) { openComment = false; } } else { if (c == ASTERISK && previous == SLASH) { openComment = true; result.deleteCharAt(result.length() - 1); } else { result.append(c); } } previous = c; } line = file.getLine(); } return result.toString(); }
RemoveComments { public String remove(FakeFile file) { if (file == null) { throw new IllegalArgumentException("You can't pass a null file as argument."); } StringBuilder result = new StringBuilder(); boolean openComment = false; String line = file.getLine(); while (line != null) { char previous = ANY_CHAR; for (char c : line.toCharArray()) { if (openComment) { if (c == SLASH && previous == ASTERISK) { openComment = false; } } else { if (c == ASTERISK && previous == SLASH) { openComment = true; result.deleteCharAt(result.length() - 1); } else { result.append(c); } } previous = c; } line = file.getLine(); } return result.toString(); } }
RemoveComments { public String remove(FakeFile file) { if (file == null) { throw new IllegalArgumentException("You can't pass a null file as argument."); } StringBuilder result = new StringBuilder(); boolean openComment = false; String line = file.getLine(); while (line != null) { char previous = ANY_CHAR; for (char c : line.toCharArray()) { if (openComment) { if (c == SLASH && previous == ASTERISK) { openComment = false; } } else { if (c == ASTERISK && previous == SLASH) { openComment = true; result.deleteCharAt(result.length() - 1); } else { result.append(c); } } previous = c; } line = file.getLine(); } return result.toString(); }  }
RemoveComments { public String remove(FakeFile file) { if (file == null) { throw new IllegalArgumentException("You can't pass a null file as argument."); } StringBuilder result = new StringBuilder(); boolean openComment = false; String line = file.getLine(); while (line != null) { char previous = ANY_CHAR; for (char c : line.toCharArray()) { if (openComment) { if (c == SLASH && previous == ASTERISK) { openComment = false; } } else { if (c == ASTERISK && previous == SLASH) { openComment = true; result.deleteCharAt(result.length() - 1); } else { result.append(c); } } previous = c; } line = file.getLine(); } return result.toString(); }  String remove(FakeFile file); }
RemoveComments { public String remove(FakeFile file) { if (file == null) { throw new IllegalArgumentException("You can't pass a null file as argument."); } StringBuilder result = new StringBuilder(); boolean openComment = false; String line = file.getLine(); while (line != null) { char previous = ANY_CHAR; for (char c : line.toCharArray()) { if (openComment) { if (c == SLASH && previous == ASTERISK) { openComment = false; } } else { if (c == ASTERISK && previous == SLASH) { openComment = true; result.deleteCharAt(result.length() - 1); } else { result.append(c); } } previous = c; } line = file.getLine(); } return result.toString(); }  String remove(FakeFile file);  }