GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix swapcase() by joshuamegnauth54 · Pull Request #7788 · RustPython/RustPython

Fix swapcase() #7788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
youknowone merged 1 commit into RustPython:main from joshuamegnauth54:swapcase-fixes
May 13, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

Fix swapcase() #7788

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
b7ad7c4
Fix swapcase()
joshuamegnauth54 May 5, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

1 change: 0 additions & 1 deletion 1 Lib/test/test_str.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,6 @@ def test_title(self):
self.assertEqual('A\u03a3 \u1fa1xy'.title(), 'A\u03c2 \u1fa9xy')
self.assertEqual('A\u03a3A'.title(), 'A\u03c3a')

@unittest.expectedFailure # TODO: RUSTPYTHON; + 𐐧
def test_swapcase(self):
string_tests.StringLikeTest.test_swapcase(self)
self.assertEqual('\U0001044F'.swapcase(), '\U00010427')
Expand Down
59 changes: 45 additions & 14 deletions 59 crates/vm/src/builtins/str.rs
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ use crate::{
TryFromBorrowedObject, VirtualMachine,
anystr::{self, AnyStr, AnyStrContainer, AnyStrWrapper, adjust_indices},
atomic_func,
bytes_inner::{swapcase_ascii, title_ascii},
cformat::cformat_string,
class::PyClassImpl,
common::lock::LazyLock,
common::str::{PyKindStr, StrData, StrKind},
common::{
lock::LazyLock,
str::{PyKindStr, StrData, StrKind},
},
convert::{IntoPyException, ToPyException, ToPyObject, ToPyResult},
format::{format, format_map},
function::{ArgIterable, ArgSize, FuncArgs, OptionalArg, OptionalOption, PyComparisonValue},
Expand Down Expand Up @@ -1078,7 +1081,7 @@ impl PyStr {
fn title(&self) -> Wtf8Buf {
match self.as_str_kind() {
PyKindStr::Ascii(_) => unsafe {
Wtf8Buf::from_bytes_unchecked(crate::bytes_inner::title_ascii(self.as_bytes()))
Wtf8Buf::from_bytes_unchecked(title_ascii(self.as_bytes()))
},
PyKindStr::Utf8(s) => {
let mut out = VecFmtWriter(Vec::with_capacity(s.len()));
Expand All @@ -1102,19 +1105,29 @@ impl PyStr {

#[pymethod]
fn swapcase(&self) -> Wtf8Buf {
let mut swapped_str = Wtf8Buf::with_capacity(self.data.len());
for c_orig in self.as_wtf8().code_points() {
let c = c_orig.to_char_lossy();
// to_uppercase returns an iterator because case changes may be multiple bytes
if c.is_lowercase() {
swapped_str.extend(c.to_uppercase());
} else if c.is_uppercase() {
swapped_str.extend(c.to_lowercase());
} else {
swapped_str.push(c_orig);
match self.as_str_kind() {
PyKindStr::Ascii(s) => unsafe {
// SAFETY: ASCII is valid Unicode and swapcase_ascii does not produce non-ASCII.
Wtf8Buf::from_bytes_unchecked(swapcase_ascii(s.as_bytes()))
},
PyKindStr::Utf8(s) => {
let mut out = VecFmtWriter(Vec::with_capacity(s.len()));
swapcase_utf8(s, &mut out);
// SAFETY: `s` is valid UTF-8 and swapcase_utf8 only works on Unicode.
unsafe { Wtf8Buf::from_bytes_unchecked(out.0) }
}
PyKindStr::Wtf8(s) => {
let mut out = VecFmtWriter(Vec::with_capacity(s.len()));
for chunk in s.as_bytes().utf8_chunks() {
swapcase_utf8(chunk.valid(), &mut out);
out.0.extend(chunk.invalid());
}
// SAFETY:
// * `s` is valid WTF-8; surrogate bytes were appended without processing.
// * swapcase_utf8 produces valid UTF-8.
unsafe { Wtf8Buf::from_bytes_unchecked(out.0) }
}
}
swapped_str
}

#[pymethod]
Expand Down Expand Up @@ -1648,6 +1661,24 @@ fn handle_capital_sigma(s: &str, i: usize) -> char {
if before && !after { 'ς' } else { 'σ' }
}

fn swapcase_utf8(s: &str, out: &mut VecFmtWriter) {
for (i, ch) in s.char_indices() {
if ch.is_uppercase() {
lowercase_or_sigma(ch, s, i, out);
} else if ch.is_lowercase() {
for ch in ch.to_uppercase() {
let mut buf = [0u8; 4];
let s = ch.encode_utf8(&mut buf);
out.0.extend(s.as_bytes());
}
} else {
let mut buf = [0u8; 4];
let s = ch.encode_utf8(&mut buf);
out.0.extend(s.as_bytes());
}
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.


impl PyRef<PyStr> {
#[must_use]
pub fn is_empty(&self) -> bool {
Expand Down
17 changes: 8 additions & 9 deletions 17 crates/vm/src/bytes_inner.rs
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -413,15 +413,7 @@ impl PyBytesInner {
}

pub fn swapcase(&self) -> Vec<u8> {
let mut new: Vec<u8> = Vec::with_capacity(self.elements.len());
for w in &self.elements {
match w {
b'A'..=b'Z' => new.push(w.to_ascii_lowercase()),
b'a'..=b'z' => new.push(w.to_ascii_uppercase()),
x => new.push(*x),
}
}
new
swapcase_ascii(self.as_bytes())
}

pub fn hex(
Expand Down Expand Up @@ -1238,3 +1230,10 @@ pub(crate) fn title_ascii(bytes: &[u8]) -> Vec<u8> {
}
out
}

pub(crate) fn swapcase_ascii(bytes: &[u8]) -> Vec<u8> {
bytes
.iter()
.map(|&b| if b.is_ascii_alphabetic() { b ^ 0x20 } else { b })
.collect()
}
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.