Scoop API

scoop:scoop.charset:0.1.0

src/options.scoop

Selected production source · src/options.scoop

1package scoop.charset
3import scoop.bytes.ByteOrder
5/**
6 * How decoding treats an initial byte-order mark.
7 */
8public enum CharsetBomPolicy {
9 /**
10 * Uses an initial BOM for decoding without preserving it as a character.
11 */
12 Consume,
13 /**
14 * Preserves an initial BOM as a Unicode character in decoded text.
15 */
16 Preserve,
17 /**
18 * Requires the input to begin with the appropriate BOM.
19 */
20 Require }
21/**
22 * Whether malformed input or an unrepresentable output character aborts conversion or is replaced.
23 */
24public enum CharsetErrorAction {
25 /**
26 * Stops conversion and reports a structured [CharsetException].
27 */
28 Strict,
29 /**
30 * Continues conversion using the backend's replacement representation.
31 */
32 Replace }
33/**
34 * The condition required to continue an incremental conversion.
35 */
36public enum CharsetState {
37 /**
38 * Provide more input, or mark the final input when the source has ended.
39 */
40 NeedInput,
41 /**
42 * Drain or replace the output buffer before continuing.
43 */
44 NeedOutput,
45 /**
46 * The final input and encoder state have been completely emitted.
47 */
48 Finished }
50/**
51 * BOM handling and fallback byte order for decoding. Explicit-endian encodings retain their own
52 * byte-order contract.
53 */
54public class CharsetDecodeOptions public constructor(
55 /**
56 * The policy applied to an initial byte-order mark.
57 */
58 public val bom: CharsetBomPolicy = CharsetBomPolicy.Consume,
59 /**
60 * Fallback order for an encoding whose byte order must otherwise be determined from a BOM.
61 */
62 public val defaultByteOrder: ByteOrder? = None
65/**
66 * BOM emission and byte-order selection for encoding Unicode text.
67 */
68public class CharsetEncodeOptions public constructor(
69 /**
70 * Whether to emit an initial byte-order mark when supported by the encoding.
71 */
72 public val emitBom: Boolean = false,
73 /**
74 * The selected byte order for an encoding that requires an explicit choice.
75 */
76 public val byteOrder: ByteOrder? = None
79/**
80 * Independent source-decoding and destination-encoding policies for a conversion. Malformed source
81 * bytes and unmappable output characters have separate error actions.
82 */
83public class CharsetTranscodeOptions public constructor(
84 /**
85 * The source BOM and byte-order policy.
86 */
87 public val decode: CharsetDecodeOptions = CharsetDecodeOptions(),
88 /**
89 * The destination BOM and byte-order policy.
90 */
91 public val encode: CharsetEncodeOptions = CharsetEncodeOptions(),
92 /**
93 * How to handle source bytes that do not form valid encoded text.
94 */
95 public val malformedInput: CharsetErrorAction = CharsetErrorAction.Strict,
96 /**
97 * How to handle valid characters unavailable in the destination encoding.
98 */
99 public val unmappableOutput: CharsetErrorAction = CharsetErrorAction.Strict
102/**
103 * Progress from one incremental conversion call.
104 *
105 * @property consumed The number of input bytes consumed by this call.
106 * @property produced The number of output bytes appended by this call.
107 * @property state Whether the caller must provide input, provide output space, or finish.
108 */
109public struct CharsetStep(val consumed: Long, val produced: Long, val state: CharsetState)
111internal fun charsetOptions(options: CharsetTranscodeOptions): NativeCharsetOptions = NativeCharsetOptions(
112 when (options.decode.bom) { case Consume -> 0; case Preserve -> 1; case Require -> 2 },
113 charsetOrder(options.decode.defaultByteOrder), charsetOrder(options.encode.byteOrder),
114 if (options.encode.emitBom) 1 else 0,
115 if (options.malformedInput == CharsetErrorAction.Replace) 1 else 0,
116 if (options.unmappableOutput == CharsetErrorAction.Replace) 1 else 0
119private fun charsetOrder(order: ByteOrder?): Int = when (order) {
120 case None -> 0
121 case Some(ByteOrder.BigEndian) -> 1
122 case Some(ByteOrder.LittleEndian) -> 2