<commit_before>//
//  Emitter.swift
//  Yams
//
//  Created by <NAME> on 12/28/16.
//  Copyright (c) 2016 Yams. All rights reserved.
//

#if SWIFT_PACKAGE
    import CYaml
#endif
import Foundation

public func serialize_all<S>(
    nodes: S,
    canonical: Bool = false,
    indent: Int = 0,
    width: Int = 0,
    allowUnicode: Bool = false,
    lineBreak: yaml_break_t = YAML_ANY_BREAK,
    encoding: yaml_encoding_t = YAML_ANY_ENCODING,
    explicitStart: Bool = false,
    explicitEnd: Bool = false,
    version: (major: Int, minor: Int)? = nil) throws -> String
    where S: Sequence, S.Iterator.Element == Node {
        let emitter = Emitter(
            canonical: canonical,
            indent: indent,
            width: width,
            allowUnicode: allowUnicode,
            lineBreak: lineBreak,
            encoding: encoding,
            explicitStart: explicitStart,
            explicitEnd: explicitEnd,
            version: version)
        try emitter.open()
        try nodes.forEach(emitter.serialize)
        try emitter.close()
        return emitter.stream
}

public func serialize(
    node: Node,
    canonical: Bool = false,
    indent: Int = 0,
    width: Int = 0,
    allowUnicode: Bool = false,
    lineBreak: yaml_break_t = YAML_ANY_BREAK,
    encoding: yaml_encoding_t = YAML_ANY_ENCODING,
    explicitStart: Bool = false,
    explicitEnd: Bool = false,
    version: (major: Int, minor: Int)? = nil) throws -> String {
        let emitter = Emitter(
            canonical: canonical,
            indent: indent,
            width: width,
            allowUnicode: allowUnicode,
            lineBreak: lineBreak,
            encoding: encoding,
            explicitStart: explicitStart,
            explicitEnd: explicitEnd,
            version: version)
        try emitter.open()
        try emitter.serialize(node: node)
        try emitter.close()
        return emitter.stream
}

public enum EmitterError: Swift.Error {
    case invalidState(String)
}

public final class Emitter {
    public var stream = ""

    let documentStartImplicit: Int32
    let documentEndImplicit: Int32
    let encoding: yaml_encoding_t
    let version: (major: Int, minor: Int)?

    public init(canonical: Bool = false,
                indent: Int = 0,
                width: Int = 0,
                allowUnicode: Bool = false,
                lineBreak: yaml_break_t = YAML_ANY_BREAK,
                encoding: yaml_encoding_t = YAML_ANY_ENCODING,
                explicitStart: Bool = false,
                explicitEnd: Bool = false,
                version: (major: Int, minor: Int)? = nil) {
        documentStartImplicit = explicitStart ? 0 : 1
        documentEndImplicit = explicitStart ? 0 : 1
        self.encoding = encoding
        self.version = version

        // configure emitter
        yaml_emitter_initialize(&emitter)

        yaml_emitter_set_output(&self.emitter, { pointer, buffer, size in
            let buffer = UnsafeBufferPointer(start: buffer, count: size)
            let encoding: String.Encoding
            #if USE_UTF16
                encoding = .utf16BigEndian
            #else
                encoding = .utf8
            #endif
            guard let string = String(bytes: buffer, encoding: encoding) else {
                return 0
            }
            let emitter = unsafeBitCast(pointer, to: Emitter.self)
            emitter.stream.write(string)
            return 1
        }, unsafeBitCast(self, to: UnsafeMutableRawPointer.self))

        yaml_emitter_set_canonical(&emitter, canonical ? 1 : 0)
        yaml_emitter_set_indent(&emitter, Int32(indent))
        yaml_emitter_set_width(&emitter, Int32(width))
        yaml_emitter_set_unicode(&emitter, allowUnicode ? 1 : 0)
        yaml_emitter_set_break(&emitter, lineBreak)
        yaml_emitter_set_encoding(&emitter, encoding)
    }

    deinit {
        yaml_emitter_delete(&emitter)
    }

    public func open() throws {
        switch state {
        case .initialized:
            var event = yaml_event_t()
            yaml_stream_start_event_initialize(&event, encoding)
            try emit(&event)
            state = .opened
        case .opened:
            throw EmitterError.invalidState("serializer is already opened")
        case .closed:
            throw EmitterError.invalidState("serializer is closed")
        }
    }

    public func close() throws {
        switch state {
        case .initialized:
            throw EmitterError.invalidState("serializer is not opened")
        case .opened:
            var event = yaml_event_t()
            yaml_stream_end_event_initialize(&event)
            try emit(&event)
            state = .closed
        case .closed:
            break // do nothing
        }
    }

    public func serialize(node: Node) throws {
        switch state {
        case .initialized:
            throw EmitterError.invalidState("serializer is not opened")<commit_msg>Remove `encoding` option from `serialize*()` and `Emitter.init`<commit_after>//
//  Emitter.swift
//  Yams
//
//  Created by <NAME> on 12/28/16.
//  Copyright (c) 2016 Yams. All rights reserved.
//

#if SWIFT_PACKAGE
    import CYaml
#endif
import Foundation

public func serialize_all<S>(
    nodes: S,
    canonical: Bool = false,
    indent: Int = 0,
    width: Int = 0,
    allowUnicode: Bool = false,
    lineBreak: yaml_break_t = YAML_ANY_BREAK,
    explicitStart: Bool = false,
    explicitEnd: Bool = false,
    version: (major: Int, minor: Int)? = nil) throws -> String
    where S: Sequence, S.Iterator.Element == Node {
        let emitter = Emitter(
            canonical: canonical,
            indent: indent,
            width: width,
            allowUnicode: allowUnicode,
            lineBreak: lineBreak,
            explicitStart: explicitStart,
            explicitEnd: explicitEnd,
            version: version)
        try emitter.open()
        try nodes.forEach(emitter.serialize)
        try emitter.close()
        #if USE_UTF16
            return String(data: emitter.data, encoding: .utf16)!
        #else
            return String(data: emitter.data, encoding: .utf8)!
        #endif
}

public func serialize(
    node: Node,
    canonical: Bool = false,
    indent: Int = 0,
    width: Int = 0,
    allowUnicode: Bool = false,
    lineBreak: yaml_break_t = YAML_ANY_BREAK,
    explicitStart: Bool = false,
    explicitEnd: Bool = false,
    version: (major: Int, minor: Int)? = nil) throws -> String {
    return try serialize_all(
        nodes: [node],
        canonical: canonical,
        indent: indent,
        width: width,
        allowUnicode: allowUnicode,
        lineBreak: lineBreak,
        explicitStart: explicitStart,
        explicitEnd: explicitEnd,
        version: version)
}

public enum EmitterError: Swift.Error {
    case invalidState(String)
}

public final class Emitter {
    public var data = Data()

    let documentStartImplicit: Int32
    let documentEndImplicit: Int32
    let version: (major: Int, minor: Int)?

    public init(canonical: Bool = false,
                indent: Int = 0,
                width: Int = 0,
                allowUnicode: Bool = false,
                lineBreak: yaml_break_t = YAML_ANY_BREAK,
                explicitStart: Bool = false,
                explicitEnd: Bool = false,
                version: (major: Int, minor: Int)? = nil) {
        documentStartImplicit = explicitStart ? 0 : 1
        documentEndImplicit = explicitStart ? 0 : 1
        self.version = version

        // configure emitter
        yaml_emitter_initialize(&emitter)

        yaml_emitter_set_output(&self.emitter, { pointer, buffer, size in
            guard let buffer = buffer else { return 0 }
            let emitter = unsafeBitCast(pointer, to: Emitter.self)
            emitter.data.append(buffer, count: size)
            return 1
        }, unsafeBitCast(self, to: UnsafeMutableRawPointer.self))

        yaml_emitter_set_canonical(&emitter, canonical ? 1 : 0)
        yaml_emitter_set_indent(&emitter, Int32(indent))
        yaml_emitter_set_width(&emitter, Int32(width))
        yaml_emitter_set_unicode(&emitter, allowUnicode ? 1 : 0)
        yaml_emitter_set_break(&emitter, lineBreak)
        #if USE_UTF16
            yaml_emitter_set_encoding(&emitter, YAML_UTF16BE_ENCODING)
        #else
            yaml_emitter_set_encoding(&emitter, YAML_UTF8_ENCODING)
        #endif
    }

    deinit {
        yaml_emitter_delete(&emitter)
    }

    public func open() throws {
        switch state {
        case .initialized:
            var event = yaml_event_t()
            #if USE_UTF16
                yaml_stream_start_event_initialize(&event, YAML_UTF16BE_ENCODING)
            #else
                yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING)
            #endif
            try emit(&event)
            state = .opened
        case .opened:
            throw EmitterError.invalidState("serializer is already opened")
        case .closed:
            throw EmitterError.invalidState("serializer is closed")
        }
    }

    public func close() throws {
        switch state {
        case .initialized:
            throw EmitterError.invalidState("serializer is not opened")
        case .opened:
            var event = yaml_event_t()
            yaml_stream_end_event_initialize(&event)
            try emit(&event)
            state = .closed
        case .closed:
            break // do nothing
        }
    }

    public func serialize(node: Node) throws {
        switch state {
        case .initialized:
            throw EmitterError.invalidState("serializer is not opened")
{'fraction_non_alphanumeric': 0.08524318158249507, 'fraction_numerical': 0.009125790729026237, 'mean_word_length': 2.0499683744465527, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4089944', 'n_tokens_mistral': 2894, 'n_tokens_neox': 2635, 'n_words': 721}