Add safe parameter to base64.encodeMime (#20559)
* Improve `encodeMime` signature * `string` to `openArray[char or byte]` * `safe` parameter * Fix * Revert "Fix" This reverts commit a394c505c2ab751621c24fd29b17e97c01251c1f. * Remove encodeMime's openArray overload * Document the `safe` parameter * Add changelog entry
This commit is contained in:
parent
1db25ffcd3
commit
5b195ab0f0
2 changed files with 12 additions and 3 deletions
|
|
@ -134,6 +134,7 @@
|
||||||
`toggleAttribute`, and `matches` to `std/dom`.
|
`toggleAttribute`, and `matches` to `std/dom`.
|
||||||
- Added [`jsre.hasIndices`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/hasIndices)
|
- Added [`jsre.hasIndices`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/hasIndices)
|
||||||
- Added `capacity` for `string` and `seq` to return the current capacity, see https://github.com/nim-lang/RFCs/issues/460
|
- Added `capacity` for `string` and `seq` to return the current capacity, see https://github.com/nim-lang/RFCs/issues/460
|
||||||
|
- Added `safe` parameter to `base64.encodeMime`
|
||||||
|
|
||||||
[//]: # "Deprecations:"
|
[//]: # "Deprecations:"
|
||||||
- Deprecated `selfExe` for Nimscript.
|
- Deprecated `selfExe` for Nimscript.
|
||||||
|
|
|
||||||
|
|
@ -161,15 +161,23 @@ proc encode*[T: byte|char](s: openArray[T], safe = false): string =
|
||||||
assert encode([1'u8, 2, 3, 4, 5]) == "AQIDBAU="
|
assert encode([1'u8, 2, 3, 4, 5]) == "AQIDBAU="
|
||||||
encodeImpl()
|
encodeImpl()
|
||||||
|
|
||||||
proc encode*[T: SomeInteger and not byte](s: openArray[T], safe = false): string {.deprecated: "use `byte` or `char` instead".} =
|
proc encode*[T: SomeInteger and not byte](s: openArray[T], safe = false): string
|
||||||
|
{.deprecated: "use `byte` or `char` instead".} =
|
||||||
encodeImpl()
|
encodeImpl()
|
||||||
|
|
||||||
proc encodeMime*(s: string, lineLen = 75.Positive, newLine = "\r\n"): string =
|
proc encodeMime*(s: string, lineLen = 75.Positive, newLine = "\r\n",
|
||||||
|
safe = false): string =
|
||||||
## Encodes `s` into base64 representation as lines.
|
## Encodes `s` into base64 representation as lines.
|
||||||
## Used in email MIME format, use `lineLen` and `newline`.
|
## Used in email MIME format, use `lineLen` and `newline`.
|
||||||
##
|
##
|
||||||
## This procedure encodes a string according to MIME spec.
|
## This procedure encodes a string according to MIME spec.
|
||||||
##
|
##
|
||||||
|
## If `safe` is `true` then it will encode using the
|
||||||
|
## URL-Safe and Filesystem-safe standard alphabet characters,
|
||||||
|
## which substitutes `-` instead of `+` and `_` instead of `/`.
|
||||||
|
## * https://en.wikipedia.org/wiki/Base64#URL_applications
|
||||||
|
## * https://tools.ietf.org/html/rfc4648#page-7
|
||||||
|
##
|
||||||
## **See also:**
|
## **See also:**
|
||||||
## * `encode proc<#encode,openArray[T]>`_ for encoding an openArray
|
## * `encode proc<#encode,openArray[T]>`_ for encoding an openArray
|
||||||
## * `decode proc<#decode,string>`_ for decoding a string
|
## * `decode proc<#decode,string>`_ for decoding a string
|
||||||
|
|
@ -183,7 +191,7 @@ proc encodeMime*(s: string, lineLen = 75.Positive, newLine = "\r\n"): string =
|
||||||
inc idx
|
inc idx
|
||||||
|
|
||||||
if s.len == 0: return
|
if s.len == 0: return
|
||||||
let e = encode(s)
|
let e = encode(s, safe)
|
||||||
if e.len <= lineLen or newLine.len == 0:
|
if e.len <= lineLen or newLine.len == 0:
|
||||||
return e
|
return e
|
||||||
result = newString(e.len + newLine.len * ((e.len div lineLen) - int(e.len mod lineLen == 0)))
|
result = newString(e.len + newLine.len * ((e.len div lineLen) - int(e.len mod lineLen == 0)))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue