Skip to content

replace regex with fsm #188

@Sczlog

Description

@Sczlog

I am using range_check relying on this repo to check if a string is a valid cidr, and I find it may have some performance issue,

I check the code and find ipv4.isValidCIDR just simply try to parse string to cidr and check if an error will be thrown, in the parse function it use regex to check if an string is a valid cidr.

I write a simple state machine version to check if an string is valid cidr (I know I must missing a lot of corner case and may have some bug)

const STATE_V2 = {
  IP_OCTET: 0,
  CIDR_MASK: 1,
  ERROR: 2,
};

function isValidCIDRStateMachineV2(cidr) {
  if (!cidr || cidr.length < 7 || cidr.length > 18) return false;

  let state = STATE_V2.IP_OCTET;
  let octetCount = 0;
  let octetValue = 0;
  let digitCount = 0;
  let maskValue = 0;
  let maskDigitCount = 0;
  let i = 0;

  while (i < cidr.length && state !== STATE_V2.ERROR) {
    const char = cidr.charCodeAt(i);

    if (state === STATE_V2.IP_OCTET) {
      if (char >= 48 && char <= 57) {
        // '0'-'9'
        const digit = char - 48;
        octetValue = octetValue * 10 + digit;
        digitCount++;

        if (
          digitCount > 3 ||
          octetValue > 255 ||
          (digitCount > 1 && cidr.charCodeAt(i - digitCount + 1) === 48)
        ) {
          state = STATE_V2.ERROR;
        }
      } else if (char === 46) {
        // '.'
        if (digitCount === 0 || octetCount >= 3) {
          state = STATE_V2.ERROR;
        } else {
          octetCount++;
          octetValue = 0;
          digitCount = 0;
        }
      } else if (char === 47) {
        // '/'
        if (digitCount === 0 || octetCount !== 3) {
          state = STATE_V2.ERROR;
        } else {
          octetCount++;
          state = STATE_V2.CIDR_MASK;
          digitCount = 0;
        }
      } else {
        state = STATE_V2.ERROR;
      }
    } else if (state === STATE_V2.CIDR_MASK) {
      if (char >= 48 && char <= 57) {
        // '0'-'9'
        const digit = char - 48;
        maskValue = maskValue * 10 + digit;
        maskDigitCount++;

        if (
          maskDigitCount > 2 ||
          maskValue > 32 ||
          (maskDigitCount > 1 && cidr.charCodeAt(i - maskDigitCount + 1) === 48)
        ) {
          state = STATE_V2.ERROR;
        }
      } else {
        state = STATE_V2.ERROR;
      }
    }
    i++;
  }

  return (
    state === STATE_V2.CIDR_MASK &&
    octetCount === 4 &&
    maskDigitCount > 0 &&
    maskValue <= 32
  );
}

i write a test case code by checking below if "192.168.1.0/24" is valid (also, ipaddr.js failed with prefix 0 like "192.168.01.0/24")

run with 1000000 round compare with IPV4.isValidCIDR(already warm up by running 10000 round before testing performance), got a huge performance gap.

  ipaddr.js: 1332.55 ms
  State Machine V2: 117.37 ms

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions