Skip to content

Commit 0977ad3

Browse files
committed
Add line ending text accessor
1 parent 902afbe commit 0977ad3

5 files changed

Lines changed: 20 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
pending encoder and decoder input until the wrapped writer succeeds.
5959
- Documented stream retry semantics for wrapped writer failures and
6060
finalization flush retries.
61+
- Added `LineEnding::as_str()` for allocation-free text inspection of wrapping
62+
policies.
6163

6264
## 0.8.0 - 2026-05-16
6365

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,9 @@ assert_eq!(&output[..written], b"aGVs\nbG8=");
221221

222222
Built-in policies include `LineWrap::MIME`, `LineWrap::PEM`, and
223223
`LineWrap::PEM_CRLF`. Wrapping inserts line endings between encoded lines and
224-
does not append a trailing line ending after the final line.
224+
does not append a trailing line ending after the final line. `LineEnding`
225+
exposes `as_str()`, `as_bytes()`, and `byte_len()` for allocation-free policy
226+
inspection.
225227

226228
Named profiles carry the wrapping policy for common protocols:
227229

portability/no_alloc_smoke/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ pub fn custom_profile_surfaces() -> bool {
161161
Some(wrap) => wrap,
162162
None => return false,
163163
};
164+
if wrap.line_ending.as_str() != "\n" || wrap.line_ending.as_bytes() != b"\n" {
165+
return false;
166+
}
164167
let profile = match Profile::checked_new(STANDARD, Some(wrap)) {
165168
Some(profile) => profile,
166169
None => return false,

src/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1932,15 +1932,21 @@ pub enum LineEnding {
19321932
}
19331933

19341934
impl LineEnding {
1935-
/// Returns the byte representation of this line ending.
1935+
/// Returns the text representation of this line ending.
19361936
#[must_use]
1937-
pub const fn as_bytes(self) -> &'static [u8] {
1937+
pub const fn as_str(self) -> &'static str {
19381938
match self {
1939-
Self::Lf => b"\n",
1940-
Self::CrLf => b"\r\n",
1939+
Self::Lf => "\n",
1940+
Self::CrLf => "\r\n",
19411941
}
19421942
}
19431943

1944+
/// Returns the byte representation of this line ending.
1945+
#[must_use]
1946+
pub const fn as_bytes(self) -> &'static [u8] {
1947+
self.as_str().as_bytes()
1948+
}
1949+
19441950
/// Returns the byte length of this line ending.
19451951
#[must_use]
19461952
pub const fn byte_len(self) -> usize {

tests/rfc4648.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,8 @@ fn wrapped_encoded_len_accounts_for_inserted_line_endings() {
11381138
);
11391139
assert_eq!(LineEnding::Lf.as_bytes(), b"\n");
11401140
assert_eq!(LineEnding::CrLf.as_bytes(), b"\r\n");
1141+
assert_eq!(LineEnding::Lf.as_str(), "\n");
1142+
assert_eq!(LineEnding::CrLf.as_str(), "\r\n");
11411143
assert_eq!(LineEnding::Lf.byte_len(), 1);
11421144
assert_eq!(LineEnding::CrLf.byte_len(), 2);
11431145
assert_eq!(LineWrap::MIME.line_len, 76);

0 commit comments

Comments
 (0)