<p>In C our options for converting numbers to strings and adding them to a string are limited to:</p>
<ul>
<li>Write your own function.</li>
<li><code>sprintf()</code> to a buffer.</li>
</ul>
<p>Either way, one will have to allocate a block of memory big enough and call an out-of-line function. The out-of-line function will have to either write to a preallocated buffer or write to a static object, requiring a copy. Both are sub-optimal.</p>
<p>One could manually do this the optimal way by measuring the length the numbers will require first, allocate the string, and then convert/build the string. This may be faster and more efficient but is cumbersome.</p>
<p>I am trying to figure out elegant semantics to achieve the above automatically. As far as I can tell an out-of-line library function is out because that would entail either a copy or guessing a maximum size. To achieve the optimal way, where no memory is wasted and no unnecessary copies are performed, there will have to be a construct that is automatically inlined.</p>
<p>Some languages have direct syntactic means to achieve this:</p>
<pre><code>let x = 10;
let str = &quot;x: \(x)&quot;
</code></pre>
<p>However that syntax does not allow one to specify radix or minimum digits or similar conversion options.</p>
<p>What would be an elegant syntax for a C-style language to allow fast conversion of numbers to string?</p>

<syntax><numbers><strings><optimization><type-conversion>
Implementing a number to string semantics efficiently in a C-style language?
<blockquote>
<p>Some languages have direct syntactic means to achieve this:</p>
<pre class="lang-swift prettyprint-override"><code>let x = 10;
let str = &quot;x: \(x)&quot;
</code></pre>
<p>However that syntax does not allow one to specify radix or minimum digits or similar conversion options</p>
</blockquote>
<p>In Swift (which uses the exact syntax you used in the question), it actually does. It's not a built-in overload, but <code>\(...)</code> compiles to a method call, and you can add your own overloads for it:</p>
<pre class="lang-swift prettyprint-override"><code>extension DefaultStringInterpolation {
  mutating func appendInterpolation&lt;T: BinaryInteger&gt;(_ x: T, radix: Int) {
    self.appendInterpolation(String(x, radix: radix))
  }
}

print(&quot;Hello, \(24, radix: 8)&quot;)
</code></pre>
<p><a href="https://swiftfiddle.com/hvom2go2o5f7vko3m3f2uyredm" rel="nofollow noreferrer">SwiftFiddle Link</a></p>

CC BY-SA 4.0
2023-05-17T02:04:03.003Z
2023-05-20T12:57:09.550Z
2023-05-20T12:57:09.550Z
Bbrk24
# Implementing a number to string semantics efficiently in a C-style language?
Posted by: username_0 on 2023-05-17
In C our options for converting numbers to strings and adding them to a string are limited to:

Write your own function.
sprintf() to a buffer.

Either way, one will have to allocate a block of memory big enough and call an out-of-line function. The out-of-line function will have to either write to a preallocated buffer or write to a static object, requiring a copy. Both are sub-optimal.
One could manually do this the optimal way by measuring the length the numbers will require first, allocate the string, and then convert/build the string. This may be faster and more efficient but is cumbersome.
I am trying to figure out elegant semantics to achieve the above automatically. As far as I can tell an out-of-line library function is out because that would entail either a copy or guessing a maximum size. To achieve the optimal way, where no memory is wasted and no unnecessary copies are performed, there will have to be a construct that is automatically inlined.
Some languages have direct syntactic means to achieve this:
let x = 10;
let str = "x: \(x)"

However that syntax does not allow one to specify radix or minimum digits or similar conversion options.
What would be an elegant syntax for a C-style language to allow fast conversion of numbers to string?
**Tags:** syntax numbers strings optimization type-conversion
---
## 1 answers
### Answer by username_1 on 2023-05-17. Score: 3 | Accepted answer
Some languages have direct syntactic means to achieve this:
let x = 10;
let str = "x: \(x)"

However that syntax does not allow one to specify radix or minimum digits or similar conversion options

In Swift (which uses the exact syntax you used in the question), it actually does. It's not a built-in overload, but \(...) compiles to a method call, and you can add your own overloads for it:
extension DefaultStringInterpolation {
  mutating func appendInterpolation(_ x: T, radix: Int) {
    self.appendInterpolation(String(x, radix: radix))
  }
}

print("Hello, \(24, radix: 8)")

SwiftFiddle Link
---
