<commit_before>/*
* Released under the MIT License (MIT), http://opensource.org/licenses/MIT
*
* Copyright (c) 2015 <NAME>, NotTooBad Software (nottoobadsoftware.com)
*
*/

import Foundation
<commit_msg>Add read and write methods to NSFilehandle.<commit_after>/*
* Released under the MIT License (MIT), http://opensource.org/licenses/MIT
*
* Copyright (c) 2015 <NAME>, NotTooBad Software (nottoobadsoftware.com)
*
*/

import Foundation

extension NSFileHandle {

	public func readSome (encoding encoding: UInt = main.encoding) -> String? {
		let data: NSData = self.availableData

		guard data.length > 0 else { return nil }
		guard let result = NSString(data: data, encoding: encoding) else {
			printErrorAndExit("Fatal error - could not convert stream to text.")
		}

		return result as String
	}

	public func read (encoding encoding: UInt = main.encoding) -> String {
		let data: NSData = self.readDataToEndOfFile()
		
		guard let result = NSString(data: data, encoding: encoding) else {
			printErrorAndExit("Fatal error - could not convert stream to text.")
		}

		return result as String
	}
}

extension NSFileHandle {

	public func write (string: String, encoding: UInt = main.encoding) {
		writeData(string.dataUsingEncoding(encoding, allowLossyConversion:false)!)
	}

	public func writeln (string: String = "", encoding: UInt = main.encoding) {
		self.write(string + "\n", encoding: encoding)
	}
}
{'fraction_non_alphanumeric': 0.11221590909090909, 'fraction_numerical': 0.006392045454545455, 'mean_word_length': 4.357414448669202, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, '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': '3714504', 'n_tokens_mistral': 456, 'n_tokens_neox': 414, 'n_words': 136}