update static method

int update(
  1. int val,
  2. String upd
)

Updates mode with newly given permissions

Implementation

static int update(int val, String upd) {
  if (!(upd is String)) {
    return val;
  }

  var action = upd[0];

  if (action == '+' || action == '-') {
    var val0 = val;

    // Split delta-string like '+ABC-DEF+Z' into an array of parts including + and -.
    var parts = upd.split(RegExp(r'([-+])'));
    var actions = upd.split(RegExp(r'\w+'));

    actions = actions.where((value) {
      return value != '';
    }).toList();

    parts = parts.where((value) {
      return value != '';
    }).toList();

    for (var i = 0; i < parts.length; i++) {
      var action = actions[i];
      var m0 = AccessMode.decode(parts[i]);
      if (m0 == INVALID) {
        return val;
      }
      if (m0 == null) {
        continue;
      }
      if (action == '+') {
        val0 |= m0;
      } else if (action == '-') {
        val0 &= ~m0;
      }
    }
    val = val0;
  } else {
    // The string is an explicit new value 'ABC' rather than delta.
    var val0 = AccessMode.decode(upd);
    if (val0 != INVALID) {
      val = val0 ?? 0;
    }
  }

  return val;
}