find-name-start.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. 'use strict'
  2. // Attribute types must start with an ASCII alphanum character.
  3. // https://www.rfc-editor.org/rfc/rfc4514#section-3
  4. // https://www.rfc-editor.org/rfc/rfc4512#section-1.4
  5. const isLeadChar = (c) => /[a-zA-Z0-9]/.test(c) === true
  6. /**
  7. * Find the starting position of an attribute type (name). Leading spaces and
  8. * commas are ignored. If an invalid leading character is encountered, an
  9. * invalid position will be returned.
  10. *
  11. * @param {Buffer} searchBuffer
  12. * @param {number} startPos
  13. *
  14. * @returns {number} The position in the buffer where the name starts, or `-1`
  15. * if an invalid name starting character is encountered.
  16. */
  17. module.exports = function findNameStart ({ searchBuffer, startPos }) {
  18. let pos = startPos
  19. while (pos < searchBuffer.byteLength) {
  20. if (searchBuffer[pos] === 0x20 || searchBuffer[pos] === 0x2c) {
  21. // Skip leading space and comma.
  22. pos += 1
  23. continue
  24. }
  25. const char = String.fromCharCode(searchBuffer[pos])
  26. if (isLeadChar(char) === true) {
  27. return pos
  28. }
  29. break
  30. }
  31. return -1
  32. }