<commit_before>//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

//===----------------------------------------------------------------------===//
// Input/Output interfaces
//===----------------------------------------------------------------------===//

/// Models an object into into which we can stream text.
@public protocol OutputStream {
  mutating
  func write(string: String)
}

/// Models an object that can be written to an `OutputStream` in a single,
/// immediately obvious, way.
///
/// For example: `String`, `Character`, `UnicodeScalar`.
@public protocol Streamable {
  func writeTo<Target : OutputStream>(inout target: Target)
}

/// This protocol should be adopted by types that wish to customize their
/// textual representation.  This textual representation is used when objects
/// are written to an `OutputStream`.
@public protocol Printable {
  var description: String { get }
}

/// This protocol should be adopted by types that wish to customize their
/// textual representation used for debugging purposes.  This textual
/// representation is used when objects are written to an `OutputStream`.
@public protocol DebugPrintable {
  var debugDescription: String { get }
}

// This protocol is adopted only by NSObject.  This is a workaround for:
// <rdar://problem/16883288> Property of type 'String!' does not satisfy
// protocol requirement of type 'String'
@public protocol _PrintableNSObject {
  var description: String! { get }
  var debugDescription: String! { get }
}
// end workaround

//===----------------------------------------------------------------------===//
// `print`
//===----------------------------------------------------------------------===//

/// Do our best to print a value that can not be printed directly, using one of
/// its conformances to `Streamable`, `Printable` or `DebugPrintable`.
func _adHocPrint<T, TargetStream : OutputStream>(
    object: T, inout target: TargetStream
) {
  var mirror = reflect(object)
  // Checking the mirror kind is not a good way to implement this, but we don't
  // have a more expressive reflection API now.
  if mirror is _TupleMirror {
    print("(", &target)
    var first = true
    for i in 0..<mirror.count {
      if first {
        first = false
      } else {
        print(", ", &target)
      }
      var (label, elementMirror) = mirror[i]
      var elt = elementMirror.value
      // FIXME: uncomment for a compiler crash:
      //_adHocPrint(elt, &target)
      // workaround:
      print(elt, &target)
    }
    print(")", &target)
    return
  }
  print(mirror.summary, &target)
}

/// Writes the textual representation of `object` into the stream `target`.
///
/// The textual representation is obtained from the `object` using its protocol
/// conformances, in the following order of preference: `Streamable`,
/// `Printable`, `DebugPrintable`.
///
/// Do not overload this function for your type.  Instead, adopt one of the
/// protocols mentioned above.
@public func print<T, TargetStream : OutputStream>(
    object: T, inout target: TargetStream
) {
  if let streamableObject =
      _stdlib_dynamicCastToExistential1(object, Streamable.self) {
    streamableObject.writeTo(&target)
    return
  }

  if var printableObject =
      _stdlib_dynamicCastToExistential1(object, Printable.self) {
    printableObject.description.writeTo(&target)
    return
  }

  if let debugPrintableObject =
      _stdlib_dynamicCastToExistential1(object, DebugPrintable.self) {
    debugPrintableObject.debugDescription.writeTo(&target)
    return
  }

  if let anNSObject =
      _stdlib_dynamicCastToExistential1(object, _PrintableNSObject.self) {
    anNSObject.description.writeTo(&target)
    return
  }

  _adHocPrint(object, &target)
}

/// Writes the textual representation of `object` and a newline character into
/// the stream `target`.
///
/// The textual representation is obtained from the `object` using its protocol
/// conformances, in the following order of preference: `Streamable`,
/// `Printable`, `DebugPrintable`.
///
/// Do not overload this function for your type.  Instead, adopt one of the
/// protocols mentioned above.
@public func println<T, TargetStream : OutputStream>(
    object: T, inout target: TargetStream
) {
  print(object, &target)
  target.write("\n")
}

/// Writes the textual representation of `object` into the stream `target`.
///
/// The textual representation is obtained from the `object` using its protocol
/// conformances, in the following order of preference: `Streamable`,
/// `Printable`, `DebugPrintable`.
///
/// Do not overload this function for your type.  Instead, adopt one of the
/// protocols mentioned above.
@public func print<T>(object: T) {
  var stdoutStream = _Stdout()
  print(object, &stdoutStream)
}

/// Writes the textual representation of `object` and a newline character into
/// the stream `target`.
///
/// The textual representation is obtained from the `object` using its protocol
/// conformances, in the following order of preference: `Streamable`,
/// `Printable`, `DebugPrintable`.
///
/// Do not overload this function for your type.  Instead, adopt one of the
/// protocols mentioned above.
@public func println<T>(object: T) {
  var stdoutStream = _Stdout()
  print(object, &stdoutStream)
  stdoutStream.write("\n")
}

/// Writes a single newline character into stdout.
@public func println() {
  var stdoutStream = _Stdout()
  stdoutStream.write("\n")
}

@public func toString<T>(object: T) -> String {
  var result = ""
  print(object, &result)
  return result
}

//===----------------------------------------------------------------------===//
// `debugPrint`
//===----------------------------------------------------------------------===//

@public func debugPrint<T, TargetStream : OutputStream>(
    object: T, inout target: TargetStream
) {
  if let debugPrintableObject =
      _stdlib_dynamicCastToExistential1(object, DebugPrintable.self) {
    debugPrintableObject.debugDescription.writeTo(&target)
    return
  }<commit_msg>stdlib/OutputStream: fix println() comments<commit_after>//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

//===----------------------------------------------------------------------===//
// Input/Output interfaces
//===----------------------------------------------------------------------===//

/// Models an object into into which we can stream text.
@public protocol OutputStream {
  mutating
  func write(string: String)
}

/// Models an object that can be written to an `OutputStream` in a single,
/// immediately obvious, way.
///
/// For example: `String`, `Character`, `UnicodeScalar`.
@public protocol Streamable {
  func writeTo<Target : OutputStream>(inout target: Target)
}

/// This protocol should be adopted by types that wish to customize their
/// textual representation.  This textual representation is used when objects
/// are written to an `OutputStream`.
@public protocol Printable {
  var description: String { get }
}

/// This protocol should be adopted by types that wish to customize their
/// textual representation used for debugging purposes.  This textual
/// representation is used when objects are written to an `OutputStream`.
@public protocol DebugPrintable {
  var debugDescription: String { get }
}

// This protocol is adopted only by NSObject.  This is a workaround for:
// <rdar://problem/16883288> Property of type 'String!' does not satisfy
// protocol requirement of type 'String'
@public protocol _PrintableNSObject {
  var description: String! { get }
  var debugDescription: String! { get }
}
// end workaround

//===----------------------------------------------------------------------===//
// `print`
//===----------------------------------------------------------------------===//

/// Do our best to print a value that can not be printed directly, using one of
/// its conformances to `Streamable`, `Printable` or `DebugPrintable`.
func _adHocPrint<T, TargetStream : OutputStream>(
    object: T, inout target: TargetStream
) {
  var mirror = reflect(object)
  // Checking the mirror kind is not a good way to implement this, but we don't
  // have a more expressive reflection API now.
  if mirror is _TupleMirror {
    print("(", &target)
    var first = true
    for i in 0..<mirror.count {
      if first {
        first = false
      } else {
        print(", ", &target)
      }
      var (label, elementMirror) = mirror[i]
      var elt = elementMirror.value
      // FIXME: uncomment for a compiler crash:
      //_adHocPrint(elt, &target)
      // workaround:
      print(elt, &target)
    }
    print(")", &target)
    return
  }
  print(mirror.summary, &target)
}

/// Writes the textual representation of `object` into the stream `target`.
///
/// The textual representation is obtained from the `object` using its protocol
/// conformances, in the following order of preference: `Streamable`,
/// `Printable`, `DebugPrintable`.
///
/// Do not overload this function for your type.  Instead, adopt one of the
/// protocols mentioned above.
@public func print<T, TargetStream : OutputStream>(
    object: T, inout target: TargetStream
) {
  if let streamableObject =
      _stdlib_dynamicCastToExistential1(object, Streamable.self) {
    streamableObject.writeTo(&target)
    return
  }

  if var printableObject =
      _stdlib_dynamicCastToExistential1(object, Printable.self) {
    printableObject.description.writeTo(&target)
    return
  }

  if let debugPrintableObject =
      _stdlib_dynamicCastToExistential1(object, DebugPrintable.self) {
    debugPrintableObject.debugDescription.writeTo(&target)
    return
  }

  if let anNSObject =
      _stdlib_dynamicCastToExistential1(object, _PrintableNSObject.self) {
    anNSObject.description.writeTo(&target)
    return
  }

  _adHocPrint(object, &target)
}

/// Writes the textual representation of `object` and a newline character into
/// the stream `target`.
///
/// The textual representation is obtained from the `object` using its protocol
/// conformances, in the following order of preference: `Streamable`,
/// `Printable`, `DebugPrintable`.
///
/// Do not overload this function for your type.  Instead, adopt one of the
/// protocols mentioned above.
@public func println<T, TargetStream : OutputStream>(
    object: T, inout target: TargetStream
) {
  print(object, &target)
  target.write("\n")
}

/// Writes the textual representation of `object` into the standard output.
///
/// The textual representation is obtained from the `object` using its protocol
/// conformances, in the following order of preference: `Streamable`,
/// `Printable`, `DebugPrintable`.
///
/// Do not overload this function for your type.  Instead, adopt one of the
/// protocols mentioned above.
@public func print<T>(object: T) {
  var stdoutStream = _Stdout()
  print(object, &stdoutStream)
}

/// Writes the textual representation of `object` and a newline character into
/// the standard output.
///
/// The textual representation is obtained from the `object` using its protocol
/// conformances, in the following order of preference: `Streamable`,
/// `Printable`, `DebugPrintable`.
///
/// Do not overload this function for your type.  Instead, adopt one of the
/// protocols mentioned above.
@public func println<T>(object: T) {
  var stdoutStream = _Stdout()
  print(object, &stdoutStream)
  stdoutStream.write("\n")
}

/// Writes a single newline character into the standard output.
@public func println() {
  var stdoutStream = _Stdout()
  stdoutStream.write("\n")
}

@public func toString<T>(object: T) -> String {
  var result = ""
  print(object, &result)
  return result
}

//===----------------------------------------------------------------------===//
// `debugPrint`
//===----------------------------------------------------------------------===//

@public func debugPrint<T, TargetStream : OutputStream>(
    object: T, inout target: TargetStream
) {
  if let debugPrintableObject =
      _stdlib_dynamicCastToExistential1(object, DebugPrintable.self) {
    debugPrintableObject.debugDescription.writeTo(&target)
    return
  }
{'fraction_non_alphanumeric': 0.1955569221308325, 'fraction_numerical': 0.0036897532477515567, 'mean_word_length': 2.4281949934123848, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '902899', 'n_tokens_mistral': 3427, 'n_tokens_neox': 3304, 'n_words': 1233}