parse static method

Dimension? parse(
  1. String? wh, [
  2. Pattern? delimiter
])

Parsers wh String, trying to split with delimiter between 2 numbers in the string.

Implementation

static Dimension? parse(String? wh, [Pattern? delimiter]) {
  if (wh == null) return null;
  delimiter ??= RegExp(r'[\sx,;]+');
  wh = wh.trim();
  if (wh.isEmpty) return null;

  var parts = wh.split(delimiter);
  if (parts.length < 2) return null;

  var params = <String>[];

  for (var i = 1; i < parts.length; ++i) {
    var prev = parts[i - 1].trim();
    var next = parts[i].trim();

    var prevC = prev.isNotEmpty ? prev[prev.length - 1] : '';
    var nextC = next.isNotEmpty ? next[0] : '';

    var prevInt = prevC.isNotEmpty || isInt(prevC);
    var nextInt = nextC.isNotEmpty || isInt(nextC);

    if (prevInt && nextInt) {
      var idxPrev = prev.lastIndexOf(_regexpNonDigit);
      if (idxPrev >= 0) prev = prev.substring(idxPrev + 1);

      var idxNext = next.indexOf(_regexpNonDigit);
      if (idxNext >= 0) next = next.substring(0, idxNext);

      params.add(prev);
      params.add(next);
    }
  }

  if (params.length < 2) return null;

  var pW = params[params.length - 2];
  var pH = params[params.length - 1];

  var w = parseInt(pW);
  var h = parseInt(pH);

  return w != null && h != null ? Dimension(w, h) : null;
}