You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> Important: We recommend upgrading to `1.4.1` (or `1.3.2+`: it introduced an important fix in floats formatting).
5
+
> Important: Version 1.4.7 is built with kotlin 1.9 and is compatible with ALL KMP platforms including experimentlas wasmJS.
6
6
7
-
Kotlin Multiplatform (also KMM) important missing tools, like sprintf with wide variety of formats, portable base64
7
+
Kotlin Multiplatform important missing tools, like sprintf with wide variety of formats, portable base64
8
8
9
9
# Why reinventing the wheel?
10
10
@@ -17,13 +17,16 @@ Please help me if you like the idea ;)
17
17
18
18
## In short, this library provides:
19
19
20
-
All 3 platforms:
20
+
All platforms (macosX64, macosArm64, iosX64, iosArm64, iosSimulatorArm64,
21
+
linuxX6, mingwX64, JVM, JS, wasmJS), in the same way:
21
22
22
-
-__Boyer-Moore based fast__`ByteArray.indexOf`
23
23
24
-
-`Stirng.sprintf` - something like C `sprinf` or JVM String.format but extended and multiplatform
24
+
-`Stirng.sprintf` - something like C `sprinf` or JVM `String.format` but with more features and multiplatform
25
+
25
26
- base64: `ByteArray.encodeToBase64()`, `ByteArray.encodeToBase64Compact()`, `String.decodeBase64()` and `ByteArray.decodeBase64Compact()`. Also URL-friendly forms: `ByteArray.encodeToBase64Url` and `String.decodeBase64Url`.
26
27
28
+
- Boyer-Moore based fast `ByteArray.indexOf`
29
+
27
30
- ByteArray tools: `getInt`, `putInt` and fast `indexOf`
28
31
- Tools to cache recalculable expressions: `CachedRefreshingValue`, `CachedExpression` and `CachedSyncExpression` for JVM (as a good multithreading is there)
29
32
- Missing `ReenterantMutex` for coroutines
@@ -48,24 +51,24 @@ dependencies {
48
51
//...
49
52
// see versions explained below, use latest release from
50
53
// 'releases' or whatever you need:
51
-
implementation("net.sergeych:mp_stools:1.4.1")
54
+
implementation("net.sergeych:mp_stools:1.4.7")
52
55
}
53
56
~~~
54
57
55
-
that's all. Now you have working `sprintf` on every MP platform ;)
58
+
That's all. Now you have working `sprintf` on every KMP platform ;)
56
59
57
60
# String tools:
58
61
59
62
## printf / sprintf!
60
63
61
-
The most popular and knonwn stromg format tool exists only on late JVM platform, so I reimplement it in
62
-
platofrm-portable way. Here are some examples, the reference is below it. We reporoduce
63
-
the [Java 11 String.format() notation](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Formatter)
64
+
The most popular and known stromg format tool exists only on the modern JVM platforms,
65
+
so I reimplement it in a platform-independent way.
66
+
Here are some examples, the reference is below it.
67
+
I reporoduced the [Java 11 String.format() notation](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Formatter)
64
68
as much as possible, with the following notable differences:
65
69
66
-
- for argument number (`%1$12s`) is is possible also to use `!` instead of `$` (as the latter should be escaped in
67
-
kotlin), e.g. `%1!12s` in that case is _also valid_
68
-
- date/time per-plaftorm locales are not yet supported, everything is in English
70
+
- for argument number (`%1$12s`) is possible also to use `!` instead of `$` (as the latter should be escaped in kotlin), e.g. `%1!12s` in that case is _also valid_
71
+
- date/time per-platform locales are not yet supported, everything is in English
69
72
- time zone abbreviations are missing (system returns valid tz id like +01:00 instead), as kotlinx.time does not
This sprintf/format implementation is safe on all platforms as it has not dependencies except
155
-
standard `Number.toString()` wich is presumably safe. Despite of its name it does not call `C` library, uses controlled
156
-
memory allocation and could not provide ovverruns (as kotlin arrays are all checked).
157
+
This sprintf/format implementation is safe on all platforms
158
+
as it has no dependencies except standard `Number.toString()`,
159
+
which is presumably safe.
160
+
Despite its name, it does not call `C` library,
161
+
uses controlled memory allocation and could not provide overruns, as kotlin arrays are all checked.
157
162
158
163
### Sprintf syntax summary
159
164
160
-
Generic fields has the following notation:
165
+
Generic format field has the following notation:
161
166
162
167
%[flags][size][.decimals]<format>
163
168
164
169
flags and size are optional, there could be several flags. The size field is used to pad the result to specified size,
165
-
padding is added with spaces before the value by default, this behavior could be changed with flags, see
166
-
below. `decimals` where applicable takes precedence over size, and determine how many decimal digits will be included,
167
-
e.g. `"%.3f".sprintf(1) == "1.000"`
170
+
padding is added with spaces before the value by default; this behavior could be changed with flags, see below. `decimals` where applicable takes precedence over size, and determines how many decimal digits will be included, e.g. `"%.3f".sprintf(1) == "1.000"`
168
171
169
172
If the argument is wider than the `size`, it is inserted as it is ignoring positioning flags and `size` field.
170
173
@@ -204,12 +207,12 @@ We support
204
207
the [Java 11 String.format() notation](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Formatter.html#syntax)
205
208
as much as possible here too.
206
209
207
-
To format a time object it is possible to use:
210
+
To format a time object, it is possible to use:
208
211
209
212
- multiplatform (recommended!) `kotlinx.datetime` classes: `Instant` and `LocalDateTime`.
210
213
- on JS platoform also javascript `Date` class instances are also ok
211
214
- on JVM platofm you can also use `java.time` classes: `java.time.Instant`, java.time.LocalDateTime` and `
212
-
java.time.ZonedDateTime` as well. Zoned date time will be converted to system default time zone (e.g. its time zone
215
+
java.time.ZonedDateTime` as well. Zoned date time will be converted to the system's default time zone (e.g., its time zone
213
216
information will be lost).
214
217
215
218
Supported are all standard format specifiers.
@@ -277,10 +280,13 @@ confuse._
277
280
278
281
## Base64
279
282
280
-
Why? Simply because under JS there is no "good" way to convert to/from ByteArray (or Uint8Array) that works well
281
-
everywhere, does not require NPM dependencies, work synchronously (I know how it could be made almost portable with
282
-
promises) and in a correct way everywhere. So the wheel is reinvented again and works same kotlin-way on all 3
283
-
platforms:
283
+
Why?
284
+
Because, for example, in JS,
285
+
there is no good way to convert to/from ByteArray
286
+
(or Uint8Array)that always works well and does not require NPM dependencies that work synchronously.
287
+
288
+
I know how it could be made almost portable with promises, though. So, here is an implementation that works well everywhere with the same interface.
0 commit comments