<commit_before>﻿//
// ByteExtensions.cs
//
// Author: <NAME> <<EMAIL>>
//
// Copyright (c) 2012 <NAME>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

using System;

namespace MimeKit {
	[Flags]
	enum CharType : ushort {
		None                  = 0,
		IsAscii               = (1 << 0),
		IsAttrChar            = (1 << 1),
		IsBlank               = (1 << 2),
		IsControl             = (1 << 3),
		IsDomainSafe          = (1 << 4),
		IsEncodedPhraseSafe   = (1 << 5),
		IsEncodedWordSafe     = (1 << 6),
		IsQuotedPrintableSafe = (1 << 7),
		IsSpace               = (1 << 8),
		IsSpecial             = (1 << 9),
		IsTokenSpecial        = (1 << 10),
		IsWhitespace          = (1 << 11),
		IsXDigit              = (1 << 12),
	}

	static class ByteExtensions
	{
		const string AttributeCharacters = "*'% "; // attribute-char from rfc2184
		const string CommentSpecials = "()\\\r";   // not allowed in comments
		const string DomainSpecials = "[]\\\r \t"; // not allowed in domains
		const string EncodedWordSpecials = "()<>@,;:\"/[]?.=_";  // rfc2047 5.1
		const string EncodedPhraseSpecials = "!*+-/=_";          // rfc2047 5.3
		const string Specials = "()<>@,;:\\\".[]";
		internal const string TokenSpecials = "()<>@,;:\\\"/[]?=";
		const string Whitespace = " \t\r\n";

		static CharType[] table = new CharType[256];

		static void RemoveFlags (string values, CharType bit)
		{
			for (int i = 0; i < values.Length; i++)
				table[(byte) values[i]] &= ~bit;
		}

		static void SetFlags (string values, CharType bit, CharType bitcopy, bool remove)
		{
			int i;

			if (remove) {
				for (i = 0; i < 128; i++)
					table[i] |= bit;

				for (i = 0; i < values.Length; i++)
					table[values[i]] &= ~bit;

				if (bitcopy != CharType.None) {
					for (i = 0; i < 256; i++) {
						if (table[i].HasFlag (bitcopy))
							table[i] &= ~bit;
					}
				}
			} else {
				for (i = 0; i < values.Length; i++)
					table[values[i]] |= bit;

				if (bitcopy != CharType.None) {
					for (i = 0; i < 256; i++) {
						if (table[i].HasFlag (bitcopy))
							table[i] |= bit;
					}
				}
			}
		}

		static ByteExtensions ()
		{
			for (int i = 0; i < 256; i++) {
				if (i < 128) {
					if (i < 32 || i == 127)
						table[i] |= CharType.IsControl;
					if (i > 32 && i < 127)
						table[i] |= CharType.IsAttrChar;
					if ((i >= 33 && i <= 60) || (i >= 62 && i <= 126) || i == 32)
						table[i] |= (CharType.IsQuotedPrintableSafe | CharType.IsEncodedWordSafe);
					if ((i >= '0' && i <= '9') || (i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z'))
						table[i] |= CharType.IsEncodedPhraseSafe;
					if ((i >= '0' && i <= '9') || (i >= 'a' && i <= 'f') || (i >= 'A' && i <= 'F'))
						table[i] |= CharType.IsXDigit;

					table[i] |= CharType.IsAscii;
				} else {
					table[i] |= CharType.IsControl;
				}
			}

			table[(int) ' '] |= CharType.IsSpace | CharType.IsBlank;
			table['\t'] |= CharType.IsQuotedPrintableSafe | CharType.IsBlank;

			SetFlags (Whitespace, CharType.IsWhitespace, CharType.None, false);
			SetFlags (TokenSpecials, CharType.IsTokenSpecial, CharType.IsControl, false);
			SetFlags (Specials, CharType.IsSpecial, CharType.None, false);
			SetFlags (DomainSpecials, CharType.IsDomainSafe, CharType.None, true);
			RemoveFlags (EncodedWordSpecials, CharType.IsEncodedWordSafe);
			RemoveFlags (AttributeCharacters + TokenSpecials, CharType.IsAttrChar);
			SetFlags (EncodedPhraseSpecials, CharType.IsEncodedPhraseSafe, CharType.None, false);
		}

		public static bool IsAscii (this byte c)
		{
			return table[c].HasFlag (CharType.IsAscii);
		}

		public static bool IsAtom (this byte c)
		{
			const CharType NonAtomFlags = CharType.IsSpecial | CharType.IsSpace | CharType.IsControl;

			return !table[c].HasFlag (NonAtomFlags);
		}

		public static bool IsAttr (this byte c)
		{
			return table[c].HasFlag (CharType.IsAttrChar);
		}

		public static bool IsBlank (this byte c)
		{
			return table[c].HasFlag (CharType.IsBlank);<commit_msg>Fixed IsAtom() to only return true if the character is truly an atom char<commit_after>﻿//
// ByteExtensions.cs
//
// Author: <NAME> <<EMAIL>>
//
// Copyright (c) 2012 <NAME>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

using System;

namespace MimeKit {
	[Flags]
	enum CharType : ushort {
		None                  = 0,
		IsAscii               = (1 << 0),
		IsAtom                = (1 << 1),
		IsAttrChar            = (1 << 2),
		IsBlank               = (1 << 3),
		IsControl             = (1 << 4),
		IsDomainSafe          = (1 << 5),
		IsEncodedPhraseSafe   = (1 << 6),
		IsEncodedWordSafe     = (1 << 7),
		IsQuotedPrintableSafe = (1 << 8),
		IsSpace               = (1 << 9),
		IsSpecial             = (1 << 10),
		IsTokenSpecial        = (1 << 11),
		IsWhitespace          = (1 << 12),
		IsXDigit              = (1 << 13),
	}

	static class ByteExtensions
	{
		const string AtomSafeCharacters = "!#$%@'*+-/=?^_`{|}~";
		const string AttributeCharacters = "*'% "; // attribute-char from rfc2184
		const string CommentSpecials = "()\\\r";   // not allowed in comments
		const string DomainSpecials = "[]\\\r \t"; // not allowed in domains
		const string EncodedWordSpecials = "()<>@,;:\"/[]?.=_";  // rfc2047 5.1
		const string EncodedPhraseSpecials = "!*+-/=_";          // rfc2047 5.3
		const string Specials = "()<>@,;:\\\".[]";
		internal const string TokenSpecials = "()<>@,;:\\\"/[]?=";
		const string Whitespace = " \t\r\n";

		static CharType[] table = new CharType[256];

		static void RemoveFlags (string values, CharType bit)
		{
			for (int i = 0; i < values.Length; i++)
				table[(byte) values[i]] &= ~bit;
		}

		static void SetFlags (string values, CharType bit, CharType bitcopy, bool remove)
		{
			int i;

			if (remove) {
				for (i = 0; i < 128; i++)
					table[i] |= bit;

				for (i = 0; i < values.Length; i++)
					table[values[i]] &= ~bit;

				if (bitcopy != CharType.None) {
					for (i = 0; i < 256; i++) {
						if (table[i].HasFlag (bitcopy))
							table[i] &= ~bit;
					}
				}
			} else {
				for (i = 0; i < values.Length; i++)
					table[values[i]] |= bit;

				if (bitcopy != CharType.None) {
					for (i = 0; i < 256; i++) {
						if (table[i].HasFlag (bitcopy))
							table[i] |= bit;
					}
				}
			}
		}

		static ByteExtensions ()
		{
			for (int i = 0; i < 256; i++) {
				if (i < 128) {
					if (i < 32 || i == 127)
						table[i] |= CharType.IsControl;
					if (i > 32 && i < 127)
						table[i] |= CharType.IsAttrChar;
					if ((i >= 33 && i <= 60) || (i >= 62 && i <= 126) || i == 32)
						table[i] |= (CharType.IsQuotedPrintableSafe | CharType.IsEncodedWordSafe);
					if ((i >= '0' && i <= '9') || (i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z'))
						table[i] |= CharType.IsEncodedPhraseSafe | CharType.IsAtom;
					if ((i >= '0' && i <= '9') || (i >= 'a' && i <= 'f') || (i >= 'A' && i <= 'F'))
						table[i] |= CharType.IsXDigit;

					table[i] |= CharType.IsAscii;
				} else {
					table[i] |= CharType.IsControl;
				}
			}

			table[(int) ' '] |= CharType.IsSpace | CharType.IsBlank;
			table['\t'] |= CharType.IsQuotedPrintableSafe | CharType.IsBlank;

			SetFlags (Whitespace, CharType.IsWhitespace, CharType.None, false);
			SetFlags (AtomSafeCharacters, CharType.IsAtom, CharType.None, false);
			SetFlags (TokenSpecials, CharType.IsTokenSpecial, CharType.IsControl, false);
			SetFlags (Specials, CharType.IsSpecial, CharType.None, false);
			SetFlags (DomainSpecials, CharType.IsDomainSafe, CharType.None, true);
			RemoveFlags (EncodedWordSpecials, CharType.IsEncodedWordSafe);
			RemoveFlags (AttributeCharacters + TokenSpecials, CharType.IsAttrChar);
			SetFlags (EncodedPhraseSpecials, CharType.IsEncodedPhraseSafe, CharType.None, false);
		}

		public static bool IsAscii (this byte c)
		{
			return table[c].HasFlag (CharType.IsAscii);
		}

		public static bool IsAtom (this byte c)
		{
			return table[c].HasFlag (CharType.IsAtom);
		}

		public static bool IsAttr (this byte c)
		{
			return table[c].HasFlag (CharType.IsAttrChar);
		}

		public static bool IsBlank (this byte c)
		{
			return table[c].HasFlag (CharType.IsBlank);
{'fraction_non_alphanumeric': 0.1540815319316948, 'fraction_numerical': 0.02003750863685717, 'mean_word_length': 2.7539829566506113, 'pattern_counts': {'":': 0, '<': 107, '<?xml version=': 0, '>': 35, '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': '497182', 'n_tokens_mistral': 4029, 'n_tokens_neox': 3343, 'n_words': 1050}