Skip to content

Conversation

@Rfontt
Copy link

@Rfontt Rfontt commented Dec 9, 2025

Add Multiplatform String.format Implementation

What's Changing

This PR adds a complete multiplatform implementation of String.format() to the Kotlin common library. It enables consistent string formatting across all Kotlin Multiplatform targets (JS, Wasm, Native) while maintaining compatibility with JVM's existing implementation.

What I'm Adding

New Features:

  • Multiplatform support: String.format() now works on JS, Wasm, Native, and JVM
  • Common format specifiers: Full support for %s, %d, %f, %c, and %%
  • Width and precision: Support for %10s, %.2f, %8.2f, %.5s patterns
  • Type safety: Proper type checking for format specifiers
  • Null handling: Graceful null"null" conversion
  • Error validation: Clear error messages for malformed format strings

Implementation Details:

  • Common code: Shared logic in commonMain for maximum code reuse
  • Platform specifics:
    • JS/Wasm/Native: New efficient implementations
    • JVM: Unchanged (uses existing java.lang.String.format)
  • Performance: O(n) parsing, minimal allocations, efficient number formatting
  • Memory: No persistent state, garbage-collection friendly

JVM Compatibility

Important: The JVM target continues to use the existing java.lang.String.format() implementation via the actual declaration in the standard library. This ensures:

  1. No breaking changes for existing JVM code
  2. Full compatibility with Java's format specification
  3. Locale support maintained (Java's locale-sensitive formatting)
  4. Performance remains optimal (uses JVM's highly optimized formatter)

The JVM implementation is completely unchanged - this PR only adds missing implementations for other platforms.

Resolving a 7-Year Wait

This PR finally addresses KT-25506 - a feature request that has been open since 2018

The Backstory:

  • 2018: Issue KT-25506 "Stdlib String.format in common" was opened
  • Years of requests: Community repeatedly asked for multiplatform string formatting
  • Workarounds: Developers had to use custom solutions or platform-specific code
  • Now: We're delivering a unified, efficient solution that works everywhere

Safety & Reliability:

  • Input validation: Clear error messages for invalid format strings
  • Type safety: Compile-time + runtime type checking
  • Edge cases: Handles nulls, special floats (NaN, Infinity), large numbers
  • Unicode support: Full UTF-16 compatibility

Usage Example

// Works on ALL platforms:
"Hello %s! You have %d new messages.".format("Alice", 5)
// → "Hello Alice! You have 5 new messages."

"Price: $%.2f".format(19.99)
// → "Price: $19.99"

"Progress: %d%% complete".format(75)
// → "Progress: 75% complete"

Related Issues

  • KT-25506: Original issue requesting common String.format()
  • Various duplicates: Multiple related requests over the years
  • Community discussions: Frequent requests on Kotlin forums and Slack

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants