stripDartComments static method

String stripDartComments(
  1. String source
)

Removes // and /* */ comments for entry-point validation.

Implementation

static String stripDartComments(String source) {
  final buffer = StringBuffer();
  var i = 0;
  while (i < source.length) {
    if (i + 1 < source.length && source[i] == '/' && source[i + 1] == '/') {
      i += 2;
      while (i < source.length && source[i] != '\n') i++;
      continue;
    }
    if (i + 1 < source.length && source[i] == '/' && source[i + 1] == '*') {
      i += 2;
      while (i + 1 < source.length &&
          !(source[i] == '*' && source[i + 1] == '/')) {
        i++;
      }
      i = i + 2 < source.length ? i + 2 : source.length;
      continue;
    }
    if (source[i] == "'" || source[i] == '"') {
      final quote = source[i];
      buffer.write(quote);
      i++;
      while (i < source.length) {
        final ch = source[i];
        buffer.write(ch);
        if (ch == '\\' && i + 1 < source.length) {
          buffer.write(source[i + 1]);
          i += 2;
          continue;
        }
        if (ch == quote) {
          i++;
          break;
        }
        i++;
      }
      continue;
    }
    buffer.write(source[i]);
    i++;
  }
  return buffer.toString();
}