Repository: prisma/prisma
File: packages/adapter-mssql/src/connection-string.ts (TypeScript)

Code before:
 * Values wrapped in curly braces like {value} are treated as literals where
 * semicolons and equals signs are not treated as delimiters.
 *
 * Important: Curly braces are only treated as quote characters when { is the
 * FIRST character of a value (after the = sign). This matches tedious behavior.
 *
 * @param str The string to split
 * @returns Array of parts split by semicolon (outside of curly braces)
 */
function splitRespectingBraces(str: string): string[] {
  const parts: string[] = []
  let current = ''
  let braceDepth = 0
  let afterEquals = false
  let valueStartIndex = -1

  for (let i = 0; i < str.length; i++) {
    const char = str[i]

    if (char === '=') {
      current += char
      afterEquals = true
      valueStartIndex = i + 1
    } else if (char === '{') {
      // Only treat { as opening brace if it's the first character of a value
      const isFirstCharOfValue = afterEquals && i === valueStartIndex
      if (isFirstCharOfValue) {
        braceDepth++
      }
      current += char
      afterEquals = false
    } else if (char === '}') {
      if (braceDepth > 0) {
        braceDepth--
      }
      current += char
      afterEquals = false
    } else if (char === ';' && braceDepth === 0) {
      parts.push(current)
      current = ''
      afterEquals = false
      valueStartIndex = -1
    } else {
      current += char
      afterEquals = false
    }
  }

  if (current) {
    parts.push(current)
  }

Review comment (suggestion):
The `afterEquals` flag seems redundant because `valueStartIndex` is already equal to `i` only immediately after the `=` character.
The tests still pass when I remove the `afterEquals` flag entirely.

Code after:
    if (char === '=') {
      current += char
      if (braceDepth === 0) {
        valueStartIndex = i + 1
      }
    } else if (char === '{') {
      // Only treat { as opening brace if it's the first character of a value
      const isFirstCharOfValue = i === valueStartIndex
      if (isFirstCharOfValue) {
        braceDepth++
      } else if (braceDepth > 0) {
        throw new Error(`Malformed connection string: nested '{' braces are not supported`)
      }
      current += char
    } else if (char === '}') {
      if (braceDepth > 0) {
        braceDepth--
      }
      current += char
    } else if (char === ';' && braceDepth === 0) {
      parts.push(current)
      current = ''
      valueStartIndex = -1
    } else {
      current += char
    }
  }

  if (current) {
    parts.push(current)
  }

  if (braceDepth !== 0) {
    throw new Error(`Malformed connection string: unclosed '{' brace (braceDepth=${braceDepth})`)
  }

  return parts
}

/**
 * Extracts the value from a parameter, removing curly braces if present.
 * If the value is wrapped in curly braces like {value}, returns value.
 * Otherwise returns the value as-is.
 * @param value The value to process
 * @returns The unescaped value
 */
function unescapeValue(value: string): string {
  const trimmed = value.trim()
  if (trimmed.startsWith('{') && trimmed.endsWith('}') && trimmed.length >= 2) {
    return trimmed.slice(1, -1)
  }