<commit_before>//===--- ArrayCast.swift - Casts and conversions for Array ----------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
//  Because NSArray is effectively an [AnyObject], casting [T] -> [U]
//  is an integral part of the bridging process and these two issues
//  are handled together.
//
//===----------------------------------------------------------------------===//

#if _runtime(_ObjC)
// FIXME: These need to be implemented even for non-objc:
// rdar://problem/18881196

internal enum _ValueOrReference {
  case Reference, Value

  internal init<T>(_: T.Type) {
    self = _isClassOrObjCExistential(T.self) ? .Reference : .Value
  }
}

internal enum _BridgeStyle {
  case Verbatim, Explicit

  internal init<T>(_: T.Type) {
   self = _isBridgedVerbatimToObjectiveC(T.self) ? .Verbatim : .Explicit
  }
}

//===--- Forced casts: [T] as! [U] ----------------------------------------===//

/// Implements `source as [TargetElement]`.
///
/// - Requires: At least one of `SourceElement` and `TargetElement` is a
///   class type or ObjC existential.  May trap for other "valid" inputs
///   when `TargetElement` is not bridged verbatim, if an element can't
///   be converted.
public func _arrayForceCast<SourceElement, TargetElement>(
  source: Array<SourceElement>
) -> Array<TargetElement> {
  switch (
    _ValueOrReference(SourceElement.self), _BridgeStyle(TargetElement.self)
  ) {
  case (.Reference, .Verbatim):
    let native = source._buffer.requestNativeBuffer()
    
    if _fastPath(native != nil) {
      if _fastPath(native!.storesOnlyElementsOfType(TargetElement.self)) {
        // A native buffer that is known to store only elements of the
        // TargetElement can be used directly
        return Array(source._buffer.castToBufferOf(TargetElement.self))
      }
      // Other native buffers must use deferred element type checking
      return Array(
        source._buffer.downcastToBufferWithDeferredTypeCheckOf(
          TargetElement.self))
    }
    // All non-native buffers use deferred element typechecking
    return Array(_immutableCocoaArray: source._buffer._asCocoaArray())
    <commit_msg>stdlib: update the doc comment to the new 'as!' syntax<commit_after>//===--- ArrayCast.swift - Casts and conversions for Array ----------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
//  Because NSArray is effectively an [AnyObject], casting [T] -> [U]
//  is an integral part of the bridging process and these two issues
//  are handled together.
//
//===----------------------------------------------------------------------===//

#if _runtime(_ObjC)
// FIXME: These need to be implemented even for non-objc:
// rdar://problem/18881196

internal enum _ValueOrReference {
  case Reference, Value

  internal init<T>(_: T.Type) {
    self = _isClassOrObjCExistential(T.self) ? .Reference : .Value
  }
}

internal enum _BridgeStyle {
  case Verbatim, Explicit

  internal init<T>(_: T.Type) {
   self = _isBridgedVerbatimToObjectiveC(T.self) ? .Verbatim : .Explicit
  }
}

//===--- Forced casts: [T] as! [U] ----------------------------------------===//

/// Implements `source as! [TargetElement]`.
///
/// - Requires: At least one of `SourceElement` and `TargetElement` is a
///   class type or ObjC existential.  May trap for other "valid" inputs
///   when `TargetElement` is not bridged verbatim, if an element can't
///   be converted.
public func _arrayForceCast<SourceElement, TargetElement>(
  source: Array<SourceElement>
) -> Array<TargetElement> {
  switch (
    _ValueOrReference(SourceElement.self), _BridgeStyle(TargetElement.self)
  ) {
  case (.Reference, .Verbatim):
    let native = source._buffer.requestNativeBuffer()
    
    if _fastPath(native != nil) {
      if _fastPath(native!.storesOnlyElementsOfType(TargetElement.self)) {
        // A native buffer that is known to store only elements of the
        // TargetElement can be used directly
        return Array(source._buffer.castToBufferOf(TargetElement.self))
      }
      // Other native buffers must use deferred element type checking
      return Array(
        source._buffer.downcastToBufferWithDeferredTypeCheckOf(
          TargetElement.self))
    }
    // All non-native buffers use deferred element typechecking
    return Array(_immutableCocoaArray: source._buffer._asCocoaArray())
    
{'fraction_non_alphanumeric': 0.1870854467420991, 'fraction_numerical': 0.00702301989855638, 'mean_word_length': 2.390873015873016, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, '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': '3085389', 'n_tokens_mistral': 1459, 'n_tokens_neox': 1375, 'n_words': 447}