Skip to content

Commit 27a5380

Browse files
committed
Improve README.md
1 parent 259435a commit 27a5380

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

README.md

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ julia> encodings()
6262
(Note that many of these are aliases for standard names.)
6363

6464
## The `Encoding` type
65-
In the examples above, the encoding was specified as a standard string. Though, in order to avoid ambiguities in multiple dispatch and to benefit from type specialization performance benefits, the package offers a special `Encoding` parametric type. Each parameterization of this type represents a character encoding. The [non-standard string literal](http://docs.julialang.org/en/stable/manual/strings/#man-non-standard-string-literals) `enc` can be used to create an instance of this type, like so: `enc"UTF-16"`.
65+
In the examples above, the encoding was specified as a standard string. Though, in order to avoid ambiguities in multiple dispatch and to increase performance via type specialization, the package offers a special `Encoding` parametric type. Each parameterization of this type represents a character encoding. The [non-standard string literal](http://docs.julialang.org/en/stable/manual/strings/#man-non-standard-string-literals) `enc` can be used to create an instance of this type, like so: `enc"UTF-16"`.
6666

6767
Since there is no ambiguity, the `encode` and `decode` functions accept either a string or an `Encoding` object. On the other hand, other functions presented below only support the latter to avoid creating conflicts with other packages extending Julia Base methods.
6868

@@ -128,32 +128,24 @@ julia> open(readcsv, path, enc"UTF-16")
128128
```
129129

130130
## Advanced Usage: `StringEncoder` and `StringDecoder`
131-
The convenience functions presented above are based on the `StringEncoder` and `StringDecoder` types, which wrap I/O streams and offer on the fly character encoding conversion facilities. They can be used directly if you need to work with encoded text on an already existing I/O stream. This can be illustrated using an `IOBuffer`:
131+
The convenience functions presented above are based on the `StringEncoder` and `StringDecoder` types, which wrap I/O streams and offer on-the-fly character encoding conversion facilities. They can be used directly if you need to work with encoded text on an already existing I/O stream. This can be illustrated using an `IOBuffer`:
132132
```julia
133133
julia> b = IOBuffer();
134134

135135
julia> s = StringEncoder(b, "UTF-16");
136136

137-
julia> write(s, "café");
137+
julia> write(s, "café"); # Encoding happens automatically here
138138

139139
julia> close(s); # Essential to complete encoding
140140

141-
julia> decode(takebuf_array(b), enc"UTF-16")
142-
"café"
143-
```
144-
145-
And the reverse operation:
146-
```julia
147-
julia> b = IOBuffer();
141+
julia> seek(b, 0); # Move to start of buffer
148142

149143
julia> s = StringDecoder(b, "UTF-16");
150144

151-
julia> write(b, encode("café", enc"UTF-16"));
152-
153-
julia> decode(takebuf_array(b), enc"UTF-16")
145+
julia> readstring(s) # Decoding happens automatically here
154146
"café"
155147
```
156148

157-
Do not forget to call `close` on `StringEncoder` and `StringDecoder` objects to release iconv resources. In the case of `StringEncoder`, this function will also call `flush`, which will write any characters still in the buffer, and possibly some control sequences (for stateful encodings).
149+
Do not forget to call `close` on `StringEncoder` and `StringDecoder` objects to finish the encoding process. For `StringEncoder`, this function calls `flush`, which writes any characters still in the buffer, and possibly some control sequences (for stateful encodings). For both `StringEncoder` and `StringDecoder`, `close` checks that there are no incomplete sequences left in the input stream, and raise an `IncompleteSequenceError` if that's the case. It will also free iconv resources immediately, instead of waiting for garbage collection.
158150

159151
Conversion currently raises an error if an invalid byte sequence is encountered in the input, or if some characters cannot be represented in the target enconding. It is not yet possible to ignore such characters or to replace them with a placeholder.

0 commit comments

Comments
 (0)