prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  PURLs for scoped NPM packages are invalid
  The package URLs for scoped NPM packages (e.g. [`@hapi/hoek`](https://osv.dev/vulnerability/GHSA-22h7-7wwg-qmgg)) are invalid. Parsing them with any package URL library fails.
  
  According to [the spec](https://github.com/package-url/purl-spec/blob/master/PURL-SPECIFICATION.rst#rules-for-each-purl-component), segments in the namespace (here: `@hapi`) must be percent-encdoded.
  
  So
  
  ```
  pkg:npm/@hapi/hoek
  ```
  
  should be
  
  ```
  pkg:npm/%40hapi/hoek
  ```
  
  On the same note, the name segment must be percent-encoded, too. I haven't encountered a PURL in OSV where the package name contains characters that'd need encoding, but if this is done for the namespace, it should be considered for the name as well.
  
  </issue>
  <code>
  [start of lib/osv/purl_helpers.py]
  1 # Copyright 2022 Google LLC
  2 #
  3 # Licensed under the Apache License, Version 2.0 (the "License");
  4 # you may not use this file except in compliance with the License.
  5 # You may obtain a copy of the License at
  6 #
  7 #      http://www.apache.org/licenses/LICENSE-2.0
  8 #
  9 # Unless required by applicable law or agreed to in writing, software
  10 # distributed under the License is distributed on an "AS IS" BASIS,
  11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12 # See the License for the specific language governing permissions and
  13 # limitations under the License.
  14 """PURL conversion utilities."""
  15 
  16 PURL_ECOSYSTEMS = {
  17     'crates.io': 'cargo',
  18     'Hex': 'hex',
  19     'Go': 'golang',
  20     'Maven': 'maven',
  21     'NuGet': 'nuget',
  22     'npm': 'npm',
  23     'Packagist': 'composer',
  24     'OSS-Fuzz': 'generic',
  25     'PyPI': 'pypi',
  26     'RubyGems': 'gem',
  27 }
  28 
  29 
  30 def package_to_purl(ecosystem, package_name):
  31   """Convert a ecosystem and package name to PURL."""
  32   purl_type = PURL_ECOSYSTEMS.get(ecosystem)
  33   if not purl_type:
  34     return None
  35 
  36   if purl_type == 'maven':
  37     # PURLs use / to separate the group ID and the artifact ID.
  38     package_name = package_name.replace(':', '/', 1)
  39 
  40   return f'pkg:{purl_type}/{package_name}'
  41
  [end of lib/osv/purl_helpers.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/lib/osv/purl_helpers.py b/lib/osv/purl_helpers.py
  --- a/lib/osv/purl_helpers.py
  +++ b/lib/osv/purl_helpers.py
  @@ -13,6 +13,8 @@
   # limitations under the License.
   """PURL conversion utilities."""
   
  +from urllib.parse import quote
  +
   PURL_ECOSYSTEMS = {
       'crates.io': 'cargo',
       'Hex': 'hex',
  @@ -27,6 +29,12 @@
   }
   
   
  +def _url_encode(package_name):
  +  """URL encode a PURL `namespace/name` or `name`."""
  +  parts = package_name.split('/')
  +  return '/'.join(quote(p) for p in parts)
  +
  +
   def package_to_purl(ecosystem, package_name):
     """Convert a ecosystem and package name to PURL."""
     purl_type = PURL_ECOSYSTEMS.get(ecosystem)
  @@ -37,4 +45,4 @@
       # PURLs use / to separate the group ID and the artifact ID.
       package_name = package_name.replace(':', '/', 1)
   
  -  return f'pkg:{purl_type}/{package_name}'
  +  return f'pkg:{purl_type}/{_url_encode(package_name)}'
  
verification_info: {"golden_diff": "diff --git a/lib/osv/purl_helpers.py b/lib/osv/purl_helpers.py\n--- a/lib/osv/purl_helpers.py\n+++ b/lib/osv/purl_helpers.py\n@@ -13,6 +13,8 @@\n # limitations under the License.\n \"\"\"PURL conversion utilities.\"\"\"\n \n+from urllib.parse import quote\n+\n PURL_ECOSYSTEMS = {\n     'crates.io': 'cargo',\n     'Hex': 'hex',\n@@ -27,6 +29,12 @@\n }\n \n \n+def _url_encode(package_name):\n+  \"\"\"URL encode a PURL `namespace/name` or `name`.\"\"\"\n+  parts = package_name.split('/')\n+  return '/'.join(quote(p) for p in parts)\n+\n+\n def package_to_purl(ecosystem, package_name):\n   \"\"\"Convert a ecosystem and package name to PURL.\"\"\"\n   purl_type = PURL_ECOSYSTEMS.get(ecosystem)\n@@ -37,4 +45,4 @@\n     # PURLs use / to separate the group ID and the artifact ID.\n     package_name = package_name.replace(':', '/', 1)\n \n-  return f'pkg:{purl_type}/{package_name}'\n+  return f'pkg:{purl_type}/{_url_encode(package_name)}'\n", "issue": "PURLs for scoped NPM packages are invalid\nThe package URLs for scoped NPM packages (e.g. [`@hapi/hoek`](https://osv.dev/vulnerability/GHSA-22h7-7wwg-qmgg)) are invalid. Parsing them with any package URL library fails.\r\n\r\nAccording to [the spec](https://github.com/package-url/purl-spec/blob/master/PURL-SPECIFICATION.rst#rules-for-each-purl-component), segments in the namespace (here: `@hapi`) must be percent-encdoded.\r\n\r\nSo\r\n\r\n```\r\npkg:npm/@hapi/hoek\r\n```\r\n\r\nshould be\r\n\r\n```\r\npkg:npm/%40hapi/hoek\r\n```\r\n\r\nOn the same note, the name segment must be percent-encoded, too. I haven't encountered a PURL in OSV where the package name contains characters that'd need encoding, but if this is done for the namespace, it should be considered for the name as well.\n", "before_files": [{"content": "# Copyright 2022 Google LLC\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#      http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"PURL conversion utilities.\"\"\"\n\nPURL_ECOSYSTEMS = {\n    'crates.io': 'cargo',\n    'Hex': 'hex',\n    'Go': 'golang',\n    'Maven': 'maven',\n    'NuGet': 'nuget',\n    'npm': 'npm',\n    'Packagist': 'composer',\n    'OSS-Fuzz': 'generic',\n    'PyPI': 'pypi',\n    'RubyGems': 'gem',\n}\n\n\ndef package_to_purl(ecosystem, package_name):\n  \"\"\"Convert a ecosystem and package name to PURL.\"\"\"\n  purl_type = PURL_ECOSYSTEMS.get(ecosystem)\n  if not purl_type:\n    return None\n\n  if purl_type == 'maven':\n    # PURLs use / to separate the group ID and the artifact ID.\n    package_name = package_name.replace(':', '/', 1)\n\n  return f'pkg:{purl_type}/{package_name}'\n", "path": "lib/osv/purl_helpers.py"}]}