mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-22 14:08:48 +00:00
🧹 Code style cleanup
This commit is contained in:
parent
e747921fb7
commit
f8541fd239
270
.editorconfig
270
.editorconfig
@ -1,195 +1,215 @@
|
||||
# All files
|
||||
[*]
|
||||
guidelines = 130
|
||||
|
||||
# C# files
|
||||
[*.cs]
|
||||
|
||||
#### Core EditorConfig Options ####
|
||||
|
||||
# Indentation and spacing
|
||||
indent_size = 4
|
||||
indent_style = space
|
||||
tab_width = 4
|
||||
indent_size=4
|
||||
indent_style=space
|
||||
tab_width=4
|
||||
|
||||
# New line preferences
|
||||
end_of_line = crlf
|
||||
insert_final_newline = true
|
||||
end_of_line=crlf
|
||||
insert_final_newline=true
|
||||
|
||||
#### .NET Coding Conventions ####
|
||||
|
||||
# Organize usings
|
||||
dotnet_separate_import_directive_groups = false
|
||||
dotnet_sort_system_directives_first = true
|
||||
dotnet_separate_import_directive_groups=false
|
||||
dotnet_sort_system_directives_first=true
|
||||
|
||||
# this. and Me. preferences
|
||||
dotnet_style_qualification_for_event = true:suggestion
|
||||
dotnet_style_qualification_for_field = true:suggestion
|
||||
dotnet_style_qualification_for_method = true:suggestion
|
||||
dotnet_style_qualification_for_property = true:suggestion
|
||||
dotnet_style_qualification_for_event=true:suggestion
|
||||
dotnet_style_qualification_for_field=true:suggestion
|
||||
dotnet_style_qualification_for_method=true:suggestion
|
||||
dotnet_style_qualification_for_property=true:suggestion
|
||||
|
||||
# Language keywords vs BCL types preferences
|
||||
dotnet_style_predefined_type_for_locals_parameters_members = true:warning
|
||||
dotnet_style_predefined_type_for_member_access = true:warning
|
||||
dotnet_style_predefined_type_for_locals_parameters_members=true:warning
|
||||
dotnet_style_predefined_type_for_member_access=true:warning
|
||||
|
||||
# Parentheses preferences
|
||||
dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:silent
|
||||
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:silent
|
||||
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:silent
|
||||
dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:silent
|
||||
dotnet_style_parentheses_in_arithmetic_binary_operators=always_for_clarity:silent
|
||||
dotnet_style_parentheses_in_other_binary_operators=always_for_clarity:silent
|
||||
dotnet_style_parentheses_in_other_operators=never_if_unnecessary:silent
|
||||
dotnet_style_parentheses_in_relational_binary_operators=always_for_clarity:silent
|
||||
|
||||
# Modifier preferences
|
||||
dotnet_style_require_accessibility_modifiers = for_non_interface_members:silent
|
||||
dotnet_style_require_accessibility_modifiers=for_non_interface_members:silent
|
||||
|
||||
# Expression-level preferences
|
||||
dotnet_style_coalesce_expression = true:suggestion
|
||||
dotnet_style_collection_initializer = true:suggestion
|
||||
dotnet_style_explicit_tuple_names = true:suggestion
|
||||
dotnet_style_null_propagation = true:suggestion
|
||||
dotnet_style_object_initializer = true:suggestion
|
||||
dotnet_style_prefer_auto_properties = true:silent
|
||||
dotnet_style_prefer_compound_assignment = true:suggestion
|
||||
dotnet_style_prefer_conditional_expression_over_assignment = true:silent
|
||||
dotnet_style_prefer_conditional_expression_over_return = true:silent
|
||||
dotnet_style_prefer_inferred_anonymous_type_member_names = false:suggestion
|
||||
dotnet_style_prefer_inferred_tuple_names = false:suggestion
|
||||
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
|
||||
dotnet_style_prefer_simplified_interpolation = true:suggestion
|
||||
dotnet_style_coalesce_expression=true:suggestion
|
||||
dotnet_style_collection_initializer=true:suggestion
|
||||
dotnet_style_explicit_tuple_names=true:suggestion
|
||||
dotnet_style_null_propagation=true:suggestion
|
||||
dotnet_style_object_initializer=true:suggestion
|
||||
dotnet_style_prefer_auto_properties=true:silent
|
||||
dotnet_style_prefer_compound_assignment=true:suggestion
|
||||
dotnet_style_prefer_conditional_expression_over_assignment=true:silent
|
||||
dotnet_style_prefer_conditional_expression_over_return=true:silent
|
||||
dotnet_style_prefer_inferred_anonymous_type_member_names=false:suggestion
|
||||
dotnet_style_prefer_inferred_tuple_names=false:suggestion
|
||||
dotnet_style_prefer_is_null_check_over_reference_equality_method=true:suggestion
|
||||
dotnet_style_prefer_simplified_interpolation=true:suggestion
|
||||
|
||||
# Field preferences
|
||||
dotnet_style_readonly_field = true:suggestion
|
||||
dotnet_style_readonly_field=true:suggestion
|
||||
|
||||
# Parameter preferences
|
||||
dotnet_code_quality_unused_parameters = all:suggestion
|
||||
dotnet_code_quality_unused_parameters=all:suggestion
|
||||
|
||||
#### C# Coding Conventions ####
|
||||
|
||||
# var preferences
|
||||
csharp_style_var_elsewhere = false:warning
|
||||
csharp_style_var_for_built_in_types = true:warning
|
||||
csharp_style_var_when_type_is_apparent = true:warning
|
||||
csharp_style_var_elsewhere=true:warning
|
||||
csharp_style_var_when_type_is_apparent=true:warning
|
||||
csharp_style_var_for_built_in_types=true:warning
|
||||
|
||||
# Expression-bodied members
|
||||
csharp_style_expression_bodied_accessors = true:suggestion
|
||||
csharp_style_expression_bodied_constructors = false:suggestion
|
||||
csharp_style_expression_bodied_indexers = true:suggestion
|
||||
csharp_style_expression_bodied_lambdas = true:silent
|
||||
csharp_style_expression_bodied_local_functions = false:silent
|
||||
csharp_style_expression_bodied_methods = false:suggestion
|
||||
csharp_style_expression_bodied_operators = true:suggestion
|
||||
csharp_style_expression_bodied_properties = false:suggestion
|
||||
csharp_style_expression_bodied_accessors=true:suggestion
|
||||
csharp_style_expression_bodied_constructors=false:suggestion
|
||||
csharp_style_expression_bodied_indexers=true:suggestion
|
||||
csharp_style_expression_bodied_lambdas=true:silent
|
||||
csharp_style_expression_bodied_local_functions=false:silent
|
||||
csharp_style_expression_bodied_methods=false:suggestion
|
||||
csharp_style_expression_bodied_operators=false:suggestion
|
||||
csharp_style_expression_bodied_properties=false:suggestion
|
||||
|
||||
# Pattern matching preferences
|
||||
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
|
||||
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
|
||||
csharp_style_prefer_switch_expression = true:suggestion
|
||||
csharp_style_pattern_matching_over_as_with_null_check=true:suggestion
|
||||
csharp_style_pattern_matching_over_is_with_cast_check=true:suggestion
|
||||
csharp_style_prefer_switch_expression=true:suggestion
|
||||
|
||||
# Null-checking preferences
|
||||
csharp_style_conditional_delegate_call = true:suggestion
|
||||
csharp_style_conditional_delegate_call=true:suggestion
|
||||
|
||||
# Modifier preferences
|
||||
csharp_prefer_static_local_function = true:suggestion
|
||||
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:silent
|
||||
csharp_prefer_static_local_function=true:suggestion
|
||||
csharp_preferred_modifier_order=public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:silent
|
||||
|
||||
# Code-block preferences
|
||||
csharp_prefer_braces = true:suggestion
|
||||
csharp_prefer_simple_using_statement = true:suggestion
|
||||
csharp_prefer_braces=true:suggestion
|
||||
csharp_prefer_simple_using_statement=true:suggestion
|
||||
|
||||
# Expression-level preferences
|
||||
csharp_prefer_simple_default_expression = true:suggestion
|
||||
csharp_style_deconstructed_variable_declaration = true:suggestion
|
||||
csharp_style_inlined_variable_declaration = true:suggestion
|
||||
csharp_style_pattern_local_over_anonymous_function = true:suggestion
|
||||
csharp_style_prefer_index_operator = true:suggestion
|
||||
csharp_style_prefer_range_operator = true:suggestion
|
||||
csharp_style_throw_expression = true:suggestion
|
||||
csharp_style_unused_value_assignment_preference = discard_variable:suggestion
|
||||
csharp_style_unused_value_expression_statement_preference = discard_variable:silent
|
||||
csharp_prefer_simple_default_expression=true:suggestion
|
||||
csharp_style_deconstructed_variable_declaration=true:suggestion
|
||||
csharp_style_inlined_variable_declaration=true:suggestion
|
||||
csharp_style_pattern_local_over_anonymous_function=true:suggestion
|
||||
csharp_style_prefer_index_operator=true:suggestion
|
||||
csharp_style_prefer_range_operator=true:suggestion
|
||||
csharp_style_throw_expression=true:suggestion
|
||||
csharp_style_unused_value_assignment_preference=discard_variable:suggestion
|
||||
csharp_style_unused_value_expression_statement_preference=discard_variable:silent
|
||||
|
||||
# 'using' directive preferences
|
||||
csharp_using_directive_placement = inside_namespace:suggestion
|
||||
csharp_using_directive_placement=inside_namespace:suggestion
|
||||
|
||||
#### C# Formatting Rules ####
|
||||
|
||||
max_line_length=130
|
||||
|
||||
# New line preferences
|
||||
csharp_new_line_before_catch = true
|
||||
csharp_new_line_before_else = true
|
||||
csharp_new_line_before_finally = true
|
||||
csharp_new_line_before_members_in_anonymous_types = true
|
||||
csharp_new_line_before_members_in_object_initializers = true
|
||||
csharp_new_line_before_open_brace = all
|
||||
csharp_new_line_between_query_expression_clauses = true
|
||||
csharp_new_line_before_catch=true
|
||||
csharp_new_line_before_else=true
|
||||
csharp_new_line_before_finally=true
|
||||
csharp_new_line_before_members_in_anonymous_types=true
|
||||
csharp_new_line_before_members_in_object_initializers=true
|
||||
csharp_new_line_before_open_brace=all
|
||||
csharp_new_line_between_query_expression_clauses=true
|
||||
|
||||
# Indentation preferences
|
||||
csharp_indent_block_contents = true
|
||||
csharp_indent_braces = false
|
||||
csharp_indent_case_contents = true
|
||||
csharp_indent_case_contents_when_block = true
|
||||
csharp_indent_labels = one_less_than_current
|
||||
csharp_indent_switch_labels = true
|
||||
csharp_indent_block_contents=true
|
||||
csharp_indent_braces=false
|
||||
csharp_indent_case_contents=true
|
||||
csharp_indent_case_contents_when_block=true
|
||||
csharp_indent_labels=one_less_than_current
|
||||
csharp_indent_switch_labels=true
|
||||
|
||||
# Space preferences
|
||||
csharp_space_after_cast = false
|
||||
csharp_space_after_colon_in_inheritance_clause = true
|
||||
csharp_space_after_comma = true
|
||||
csharp_space_after_dot = false
|
||||
csharp_space_after_keywords_in_control_flow_statements = true
|
||||
csharp_space_after_semicolon_in_for_statement = true
|
||||
csharp_space_around_binary_operators = before_and_after
|
||||
csharp_space_around_declaration_statements = false
|
||||
csharp_space_before_colon_in_inheritance_clause = true
|
||||
csharp_space_before_comma = false
|
||||
csharp_space_before_dot = false
|
||||
csharp_space_before_open_square_brackets = false
|
||||
csharp_space_before_semicolon_in_for_statement = false
|
||||
csharp_space_between_empty_square_brackets = false
|
||||
csharp_space_between_method_call_empty_parameter_list_parentheses = false
|
||||
csharp_space_between_method_call_name_and_opening_parenthesis = false
|
||||
csharp_space_between_method_call_parameter_list_parentheses = false
|
||||
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
|
||||
csharp_space_between_method_declaration_name_and_open_parenthesis = false
|
||||
csharp_space_between_method_declaration_parameter_list_parentheses = false
|
||||
csharp_space_between_parentheses = false
|
||||
csharp_space_between_square_brackets = false
|
||||
csharp_space_after_cast=false
|
||||
csharp_space_after_colon_in_inheritance_clause=true
|
||||
csharp_space_after_comma=true
|
||||
csharp_space_after_dot=false
|
||||
csharp_space_after_keywords_in_control_flow_statements=true
|
||||
csharp_space_after_semicolon_in_for_statement=true
|
||||
csharp_space_around_binary_operators=before_and_after
|
||||
csharp_space_around_declaration_statements=false
|
||||
csharp_space_before_colon_in_inheritance_clause=true
|
||||
csharp_space_before_comma=false
|
||||
csharp_space_before_dot=false
|
||||
csharp_space_before_open_square_brackets=false
|
||||
csharp_space_before_semicolon_in_for_statement=false
|
||||
csharp_space_between_empty_square_brackets=false
|
||||
csharp_space_between_method_call_empty_parameter_list_parentheses=false
|
||||
csharp_space_between_method_call_name_and_opening_parenthesis=false
|
||||
csharp_space_between_method_call_parameter_list_parentheses=false
|
||||
csharp_space_between_method_declaration_empty_parameter_list_parentheses=false
|
||||
csharp_space_between_method_declaration_name_and_open_parenthesis=false
|
||||
csharp_space_between_method_declaration_parameter_list_parentheses=false
|
||||
csharp_space_between_parentheses=false
|
||||
csharp_space_between_square_brackets=false
|
||||
|
||||
# Wrapping preferences
|
||||
csharp_preserve_single_line_blocks = true
|
||||
csharp_preserve_single_line_statements = true
|
||||
csharp_preserve_single_line_blocks=true
|
||||
csharp_preserve_single_line_statements=true
|
||||
|
||||
#### Naming styles ####
|
||||
|
||||
# Naming rules
|
||||
|
||||
dotnet_naming_rule.interface_should_be_begins_with_i.severity = suggestion
|
||||
dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface
|
||||
dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i
|
||||
dotnet_naming_rule.interface_should_be_begins_with_i.severity=suggestion
|
||||
dotnet_naming_rule.interface_should_be_begins_with_i.symbols=interface
|
||||
dotnet_naming_rule.interface_should_be_begins_with_i.style=begins_with_i
|
||||
|
||||
dotnet_naming_rule.types_should_be_pascal_case.severity = suggestion
|
||||
dotnet_naming_rule.types_should_be_pascal_case.symbols = types
|
||||
dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case
|
||||
dotnet_naming_rule.types_should_be_pascal_case.severity=suggestion
|
||||
dotnet_naming_rule.types_should_be_pascal_case.symbols=types
|
||||
dotnet_naming_rule.types_should_be_pascal_case.style=pascal_case
|
||||
|
||||
dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = suggestion
|
||||
dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members
|
||||
dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case
|
||||
dotnet_naming_rule.non_field_members_should_be_pascal_case.severity=suggestion
|
||||
dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols=non_field_members
|
||||
dotnet_naming_rule.non_field_members_should_be_pascal_case.style=pascal_case
|
||||
|
||||
# Symbol specifications
|
||||
|
||||
dotnet_naming_symbols.interface.applicable_kinds = interface
|
||||
dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
|
||||
dotnet_naming_symbols.interface.required_modifiers =
|
||||
dotnet_naming_symbols.interface.applicable_kinds=interface
|
||||
dotnet_naming_symbols.interface.applicable_accessibilities=public, internal, private, protected, protected_internal, private_protected
|
||||
dotnet_naming_symbols.interface.required_modifiers=
|
||||
|
||||
dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum
|
||||
dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
|
||||
dotnet_naming_symbols.types.required_modifiers =
|
||||
dotnet_naming_symbols.types.applicable_kinds=class, struct, interface, enum
|
||||
dotnet_naming_symbols.types.applicable_accessibilities=public, internal, private, protected, protected_internal, private_protected
|
||||
dotnet_naming_symbols.types.required_modifiers=
|
||||
|
||||
dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method
|
||||
dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
|
||||
dotnet_naming_symbols.non_field_members.required_modifiers =
|
||||
dotnet_naming_symbols.non_field_members.applicable_kinds=property, event, method
|
||||
dotnet_naming_symbols.non_field_members.applicable_accessibilities=public, internal, private, protected, protected_internal, private_protected
|
||||
dotnet_naming_symbols.non_field_members.required_modifiers=
|
||||
|
||||
# Naming styles
|
||||
|
||||
dotnet_naming_style.pascal_case.required_prefix =
|
||||
dotnet_naming_style.pascal_case.required_suffix =
|
||||
dotnet_naming_style.pascal_case.word_separator =
|
||||
dotnet_naming_style.pascal_case.capitalization = pascal_case
|
||||
dotnet_naming_style.pascal_case.required_prefix=
|
||||
dotnet_naming_style.pascal_case.required_suffix=
|
||||
dotnet_naming_style.pascal_case.word_separator=
|
||||
dotnet_naming_style.pascal_case.capitalization=pascal_case
|
||||
|
||||
dotnet_naming_style.begins_with_i.required_prefix = I
|
||||
dotnet_naming_style.begins_with_i.required_suffix =
|
||||
dotnet_naming_style.begins_with_i.word_separator =
|
||||
dotnet_naming_style.begins_with_i.capitalization = pascal_case
|
||||
dotnet_naming_style.begins_with_i.required_prefix=I
|
||||
dotnet_naming_style.begins_with_i.required_suffix=
|
||||
dotnet_naming_style.begins_with_i.word_separator=
|
||||
dotnet_naming_style.begins_with_i.capitalization=pascal_case
|
||||
|
||||
# XML comment analysis is disabled due to project configuration
|
||||
# https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA0001.md
|
||||
dotnet_diagnostic.SA0001.severity = silent
|
||||
|
||||
# A C# code file is missing a standard file header.
|
||||
# https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1633.md
|
||||
dotnet_diagnostic.SA1633.severity = silent
|
||||
|
||||
# Consider calling ConfigureAwait on the awaited Task
|
||||
# https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2007?view=vs-2019
|
||||
# https://github.com/JosefPihrt/Roslynator/blob/master/docs/analyzers/RCS1090.md
|
||||
dotnet_diagnostic.CA2007.severity = silent
|
||||
dotnet_diagnostic.RCS1090.severity = silent
|
||||
|
259
X10D.ruleset
259
X10D.ruleset
@ -1,259 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RuleSet Name="X10D" Description=" " ToolsVersion="16.0">
|
||||
<Rules AnalyzerId="Microsoft.Analyzers.ManagedCodeAnalysis" RuleNamespace="Microsoft.Rules.Managed">
|
||||
<Rule Id="CA1000" Action="Warning" />
|
||||
<Rule Id="CA1001" Action="Warning" />
|
||||
<Rule Id="CA1002" Action="Warning" />
|
||||
<Rule Id="CA1003" Action="Warning" />
|
||||
<Rule Id="CA1004" Action="Warning" />
|
||||
<Rule Id="CA1005" Action="Warning" />
|
||||
<Rule Id="CA1006" Action="Warning" />
|
||||
<Rule Id="CA1007" Action="Warning" />
|
||||
<Rule Id="CA1008" Action="Warning" />
|
||||
<Rule Id="CA1009" Action="Warning" />
|
||||
<Rule Id="CA1010" Action="Warning" />
|
||||
<Rule Id="CA1011" Action="Warning" />
|
||||
<Rule Id="CA1012" Action="Warning" />
|
||||
<Rule Id="CA1013" Action="Warning" />
|
||||
<Rule Id="CA1014" Action="Warning" />
|
||||
<Rule Id="CA1016" Action="Warning" />
|
||||
<Rule Id="CA1017" Action="Warning" />
|
||||
<Rule Id="CA1018" Action="Warning" />
|
||||
<Rule Id="CA1019" Action="Warning" />
|
||||
<Rule Id="CA1020" Action="Warning" />
|
||||
<Rule Id="CA1021" Action="Warning" />
|
||||
<Rule Id="CA1023" Action="Warning" />
|
||||
<Rule Id="CA1024" Action="Warning" />
|
||||
<Rule Id="CA1025" Action="Warning" />
|
||||
<Rule Id="CA1026" Action="Warning" />
|
||||
<Rule Id="CA1027" Action="Warning" />
|
||||
<Rule Id="CA1028" Action="Warning" />
|
||||
<Rule Id="CA1030" Action="Warning" />
|
||||
<Rule Id="CA1031" Action="Warning" />
|
||||
<Rule Id="CA1032" Action="Warning" />
|
||||
<Rule Id="CA1033" Action="Warning" />
|
||||
<Rule Id="CA1034" Action="Warning" />
|
||||
<Rule Id="CA1035" Action="Warning" />
|
||||
<Rule Id="CA1036" Action="Warning" />
|
||||
<Rule Id="CA1038" Action="Warning" />
|
||||
<Rule Id="CA1039" Action="Warning" />
|
||||
<Rule Id="CA1040" Action="Warning" />
|
||||
<Rule Id="CA1041" Action="Warning" />
|
||||
<Rule Id="CA1043" Action="Warning" />
|
||||
<Rule Id="CA1044" Action="Warning" />
|
||||
<Rule Id="CA1045" Action="Warning" />
|
||||
<Rule Id="CA1046" Action="Warning" />
|
||||
<Rule Id="CA1047" Action="Warning" />
|
||||
<Rule Id="CA1048" Action="Warning" />
|
||||
<Rule Id="CA1049" Action="Warning" />
|
||||
<Rule Id="CA1050" Action="Warning" />
|
||||
<Rule Id="CA1051" Action="Warning" />
|
||||
<Rule Id="CA1052" Action="Warning" />
|
||||
<Rule Id="CA1053" Action="Warning" />
|
||||
<Rule Id="CA1054" Action="Warning" />
|
||||
<Rule Id="CA1055" Action="Warning" />
|
||||
<Rule Id="CA1056" Action="Warning" />
|
||||
<Rule Id="CA1057" Action="Warning" />
|
||||
<Rule Id="CA1058" Action="Warning" />
|
||||
<Rule Id="CA1059" Action="Warning" />
|
||||
<Rule Id="CA1060" Action="Warning" />
|
||||
<Rule Id="CA1061" Action="Warning" />
|
||||
<Rule Id="CA1062" Action="Warning" />
|
||||
<Rule Id="CA1063" Action="Warning" />
|
||||
<Rule Id="CA1064" Action="Warning" />
|
||||
<Rule Id="CA1065" Action="Warning" />
|
||||
<Rule Id="CA1300" Action="Warning" />
|
||||
<Rule Id="CA1301" Action="Warning" />
|
||||
<Rule Id="CA1302" Action="Warning" />
|
||||
<Rule Id="CA1303" Action="Warning" />
|
||||
<Rule Id="CA1304" Action="Warning" />
|
||||
<Rule Id="CA1305" Action="Warning" />
|
||||
<Rule Id="CA1306" Action="Warning" />
|
||||
<Rule Id="CA1307" Action="Warning" />
|
||||
<Rule Id="CA1308" Action="Warning" />
|
||||
<Rule Id="CA1309" Action="Warning" />
|
||||
<Rule Id="CA1400" Action="Warning" />
|
||||
<Rule Id="CA1401" Action="Warning" />
|
||||
<Rule Id="CA1402" Action="Warning" />
|
||||
<Rule Id="CA1403" Action="Warning" />
|
||||
<Rule Id="CA1404" Action="Warning" />
|
||||
<Rule Id="CA1405" Action="Warning" />
|
||||
<Rule Id="CA1406" Action="Warning" />
|
||||
<Rule Id="CA1407" Action="Warning" />
|
||||
<Rule Id="CA1408" Action="Warning" />
|
||||
<Rule Id="CA1409" Action="Warning" />
|
||||
<Rule Id="CA1410" Action="Warning" />
|
||||
<Rule Id="CA1411" Action="Warning" />
|
||||
<Rule Id="CA1412" Action="Warning" />
|
||||
<Rule Id="CA1413" Action="Warning" />
|
||||
<Rule Id="CA1414" Action="Warning" />
|
||||
<Rule Id="CA1415" Action="Warning" />
|
||||
<Rule Id="CA1500" Action="Warning" />
|
||||
<Rule Id="CA1501" Action="Warning" />
|
||||
<Rule Id="CA1502" Action="Warning" />
|
||||
<Rule Id="CA1504" Action="Warning" />
|
||||
<Rule Id="CA1505" Action="Warning" />
|
||||
<Rule Id="CA1506" Action="Warning" />
|
||||
<Rule Id="CA1600" Action="Warning" />
|
||||
<Rule Id="CA1601" Action="Warning" />
|
||||
<Rule Id="CA1700" Action="Warning" />
|
||||
<Rule Id="CA1701" Action="Warning" />
|
||||
<Rule Id="CA1702" Action="Warning" />
|
||||
<Rule Id="CA1703" Action="Warning" />
|
||||
<Rule Id="CA1704" Action="Warning" />
|
||||
<Rule Id="CA1707" Action="Warning" />
|
||||
<Rule Id="CA1708" Action="Warning" />
|
||||
<Rule Id="CA1709" Action="Warning" />
|
||||
<Rule Id="CA1710" Action="Warning" />
|
||||
<Rule Id="CA1711" Action="Warning" />
|
||||
<Rule Id="CA1712" Action="Warning" />
|
||||
<Rule Id="CA1713" Action="Warning" />
|
||||
<Rule Id="CA1714" Action="Warning" />
|
||||
<Rule Id="CA1715" Action="Warning" />
|
||||
<Rule Id="CA1716" Action="Warning" />
|
||||
<Rule Id="CA1717" Action="Warning" />
|
||||
<Rule Id="CA1719" Action="Warning" />
|
||||
<Rule Id="CA1720" Action="Warning" />
|
||||
<Rule Id="CA1721" Action="Warning" />
|
||||
<Rule Id="CA1722" Action="Warning" />
|
||||
<Rule Id="CA1724" Action="Warning" />
|
||||
<Rule Id="CA1725" Action="Warning" />
|
||||
<Rule Id="CA1726" Action="Warning" />
|
||||
<Rule Id="CA1800" Action="Warning" />
|
||||
<Rule Id="CA1801" Action="Warning" />
|
||||
<Rule Id="CA1802" Action="Warning" />
|
||||
<Rule Id="CA1804" Action="Warning" />
|
||||
<Rule Id="CA1806" Action="Warning" />
|
||||
<Rule Id="CA1809" Action="Warning" />
|
||||
<Rule Id="CA1810" Action="Warning" />
|
||||
<Rule Id="CA1811" Action="Warning" />
|
||||
<Rule Id="CA1812" Action="Warning" />
|
||||
<Rule Id="CA1813" Action="Warning" />
|
||||
<Rule Id="CA1814" Action="Warning" />
|
||||
<Rule Id="CA1815" Action="Warning" />
|
||||
<Rule Id="CA1816" Action="Warning" />
|
||||
<Rule Id="CA1819" Action="Warning" />
|
||||
<Rule Id="CA1820" Action="Warning" />
|
||||
<Rule Id="CA1821" Action="Warning" />
|
||||
<Rule Id="CA1822" Action="Warning" />
|
||||
<Rule Id="CA1823" Action="Warning" />
|
||||
<Rule Id="CA1824" Action="Warning" />
|
||||
<Rule Id="CA1900" Action="Warning" />
|
||||
<Rule Id="CA1901" Action="Warning" />
|
||||
<Rule Id="CA1903" Action="Warning" />
|
||||
<Rule Id="CA2000" Action="Warning" />
|
||||
<Rule Id="CA2001" Action="Warning" />
|
||||
<Rule Id="CA2002" Action="Warning" />
|
||||
<Rule Id="CA2003" Action="Warning" />
|
||||
<Rule Id="CA2004" Action="Warning" />
|
||||
<Rule Id="CA2006" Action="Warning" />
|
||||
<Rule Id="CA2100" Action="Warning" />
|
||||
<Rule Id="CA2101" Action="Warning" />
|
||||
<Rule Id="CA2102" Action="Warning" />
|
||||
<Rule Id="CA2103" Action="Warning" />
|
||||
<Rule Id="CA2104" Action="Warning" />
|
||||
<Rule Id="CA2105" Action="Warning" />
|
||||
<Rule Id="CA2106" Action="Warning" />
|
||||
<Rule Id="CA2107" Action="Warning" />
|
||||
<Rule Id="CA2108" Action="Warning" />
|
||||
<Rule Id="CA2109" Action="Warning" />
|
||||
<Rule Id="CA2111" Action="Warning" />
|
||||
<Rule Id="CA2112" Action="Warning" />
|
||||
<Rule Id="CA2114" Action="Warning" />
|
||||
<Rule Id="CA2115" Action="Warning" />
|
||||
<Rule Id="CA2116" Action="Warning" />
|
||||
<Rule Id="CA2117" Action="Warning" />
|
||||
<Rule Id="CA2118" Action="Warning" />
|
||||
<Rule Id="CA2119" Action="Warning" />
|
||||
<Rule Id="CA2120" Action="Warning" />
|
||||
<Rule Id="CA2121" Action="Warning" />
|
||||
<Rule Id="CA2122" Action="Warning" />
|
||||
<Rule Id="CA2123" Action="Warning" />
|
||||
<Rule Id="CA2124" Action="Warning" />
|
||||
<Rule Id="CA2126" Action="Warning" />
|
||||
<Rule Id="CA2130" Action="Warning" />
|
||||
<Rule Id="CA2131" Action="Warning" />
|
||||
<Rule Id="CA2132" Action="Warning" />
|
||||
<Rule Id="CA2133" Action="Warning" />
|
||||
<Rule Id="CA2134" Action="Warning" />
|
||||
<Rule Id="CA2135" Action="Warning" />
|
||||
<Rule Id="CA2136" Action="Warning" />
|
||||
<Rule Id="CA2137" Action="Warning" />
|
||||
<Rule Id="CA2138" Action="Warning" />
|
||||
<Rule Id="CA2139" Action="Warning" />
|
||||
<Rule Id="CA2140" Action="Warning" />
|
||||
<Rule Id="CA2141" Action="Warning" />
|
||||
<Rule Id="CA2142" Action="Warning" />
|
||||
<Rule Id="CA2143" Action="Warning" />
|
||||
<Rule Id="CA2144" Action="Warning" />
|
||||
<Rule Id="CA2145" Action="Warning" />
|
||||
<Rule Id="CA2146" Action="Warning" />
|
||||
<Rule Id="CA2147" Action="Warning" />
|
||||
<Rule Id="CA2149" Action="Warning" />
|
||||
<Rule Id="CA2151" Action="Warning" />
|
||||
<Rule Id="CA2200" Action="Warning" />
|
||||
<Rule Id="CA2201" Action="Warning" />
|
||||
<Rule Id="CA2202" Action="Warning" />
|
||||
<Rule Id="CA2204" Action="Warning" />
|
||||
<Rule Id="CA2205" Action="Warning" />
|
||||
<Rule Id="CA2207" Action="Warning" />
|
||||
<Rule Id="CA2208" Action="Warning" />
|
||||
<Rule Id="CA2210" Action="Warning" />
|
||||
<Rule Id="CA2211" Action="Warning" />
|
||||
<Rule Id="CA2212" Action="Warning" />
|
||||
<Rule Id="CA2213" Action="Warning" />
|
||||
<Rule Id="CA2214" Action="Warning" />
|
||||
<Rule Id="CA2215" Action="Warning" />
|
||||
<Rule Id="CA2216" Action="Warning" />
|
||||
<Rule Id="CA2217" Action="Warning" />
|
||||
<Rule Id="CA2218" Action="Warning" />
|
||||
<Rule Id="CA2219" Action="Warning" />
|
||||
<Rule Id="CA2220" Action="Warning" />
|
||||
<Rule Id="CA2221" Action="Warning" />
|
||||
<Rule Id="CA2222" Action="Warning" />
|
||||
<Rule Id="CA2223" Action="Warning" />
|
||||
<Rule Id="CA2224" Action="Warning" />
|
||||
<Rule Id="CA2225" Action="Warning" />
|
||||
<Rule Id="CA2226" Action="Warning" />
|
||||
<Rule Id="CA2227" Action="Warning" />
|
||||
<Rule Id="CA2228" Action="Warning" />
|
||||
<Rule Id="CA2229" Action="Warning" />
|
||||
<Rule Id="CA2230" Action="Warning" />
|
||||
<Rule Id="CA2231" Action="Warning" />
|
||||
<Rule Id="CA2232" Action="Warning" />
|
||||
<Rule Id="CA2233" Action="Warning" />
|
||||
<Rule Id="CA2234" Action="Warning" />
|
||||
<Rule Id="CA2235" Action="Warning" />
|
||||
<Rule Id="CA2236" Action="Warning" />
|
||||
<Rule Id="CA2237" Action="Warning" />
|
||||
<Rule Id="CA2238" Action="Warning" />
|
||||
<Rule Id="CA2239" Action="Warning" />
|
||||
<Rule Id="CA2240" Action="Warning" />
|
||||
<Rule Id="CA2241" Action="Warning" />
|
||||
<Rule Id="CA2242" Action="Warning" />
|
||||
<Rule Id="CA2243" Action="Warning" />
|
||||
<Rule Id="CA5122" Action="Warning" />
|
||||
</Rules>
|
||||
<Rules AnalyzerId="Microsoft.CodeAnalysis.CSharp.Features" RuleNamespace="Microsoft.CodeAnalysis.CSharp.Features">
|
||||
<Rule Id="IDE0008" Action="None" />
|
||||
<Rule Id="IDE0008WithoutSuggestion" Action="None" />
|
||||
</Rules>
|
||||
<Rules AnalyzerId="Microsoft.NetCore.Analyzers" RuleNamespace="Microsoft.NetCore.Analyzers">
|
||||
<Rule Id="CA1813" Action="Warning" />
|
||||
</Rules>
|
||||
<Rules AnalyzerId="Roslynator.CSharp.Analyzers" RuleNamespace="Roslynator.CSharp.Analyzers">
|
||||
<Rule Id="RCS1010" Action="Warning" />
|
||||
<Rule Id="RCS1177" Action="Warning" />
|
||||
</Rules>
|
||||
<Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers">
|
||||
<Rule Id="SA1121" Action="None" />
|
||||
<Rule Id="SA1633" Action="None" />
|
||||
<Rule Id="SA1634" Action="None" />
|
||||
<Rule Id="SA1635" Action="None" />
|
||||
<Rule Id="SA1636" Action="None" />
|
||||
<Rule Id="SA1637" Action="None" />
|
||||
<Rule Id="SA1638" Action="None" />
|
||||
<Rule Id="SA1640" Action="None" />
|
||||
<Rule Id="SA1641" Action="None" />
|
||||
</Rules>
|
||||
</RuleSet>
|
@ -1,128 +1,142 @@
|
||||
namespace X10D
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="bool"/>.
|
||||
/// Extension methods for <see cref="bool" />.
|
||||
/// </summary>
|
||||
public static class BooleanExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Performs logical AND on this <see cref="bool"/> and another <see cref="bool"/>.
|
||||
/// Performs logical AND on this <see cref="bool" /> and another <see cref="bool" />.
|
||||
/// </summary>
|
||||
/// <param name="value">The boolean.</param>
|
||||
/// <param name="comparison">The boolean comparator.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="value"/> AND <paramref name="comparison"/>
|
||||
/// evaluate to <see langword="true"/>, or <see langword="false"/> otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if <paramref name="value" /> AND <paramref name="comparison" />
|
||||
/// evaluate to <see langword="true" />, or <see langword="false" /> otherwise.
|
||||
/// </returns>
|
||||
public static bool And(this bool value, bool comparison)
|
||||
{
|
||||
return value && comparison;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs logical NAND on this <see cref="bool"/> and another <see cref="bool"/>.
|
||||
/// Performs logical NAND on this <see cref="bool" /> and another <see cref="bool" />.
|
||||
/// </summary>
|
||||
/// <param name="value">The boolean.</param>
|
||||
/// <param name="comparison">The boolean comparator.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="value"/> NAND <paramref name="comparison"/>
|
||||
/// evaluate to <see langword="true"/>, or <see langword="false"/> otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if <paramref name="value" /> NAND <paramref name="comparison" />
|
||||
/// evaluate to <see langword="true" />, or <see langword="false" /> otherwise.
|
||||
/// </returns>
|
||||
public static bool NAnd(this bool value, bool comparison)
|
||||
{
|
||||
return !(value && comparison);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs logical NOR on this <see cref="bool"/> and another <see cref="bool"/>.
|
||||
/// Performs logical NOR on this <see cref="bool" /> and another <see cref="bool" />.
|
||||
/// </summary>
|
||||
/// <param name="value">The boolean.</param>
|
||||
/// <param name="comparison">The boolean comparator.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="value"/> NOR <paramref name="comparison"/>
|
||||
/// evaluate to <see langword="true"/>, or <see langword="false"/> otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if <paramref name="value" /> NOR <paramref name="comparison" />
|
||||
/// evaluate to <see langword="true" />, or <see langword="false" /> otherwise.
|
||||
/// </returns>
|
||||
public static bool NOr(this bool value, bool comparison)
|
||||
{
|
||||
return !(value || comparison);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs logical NOT on this <see cref="bool"/>.
|
||||
/// Performs logical NOT on this <see cref="bool" />.
|
||||
/// </summary>
|
||||
/// <param name="value">The boolean.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="value"/> is <see langword="false"/>,
|
||||
/// or <see langword="false"/> otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if <paramref name="value" /> is <see langword="false" />,
|
||||
/// or <see langword="false" /> otherwise.
|
||||
/// </returns>
|
||||
public static bool Not(this bool value)
|
||||
{
|
||||
return !value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs logical OR on this <see cref="bool"/> and another <see cref="bool"/>.
|
||||
/// Performs logical OR on this <see cref="bool" /> and another <see cref="bool" />.
|
||||
/// </summary>
|
||||
/// <param name="value">The boolean.</param>
|
||||
/// <param name="comparison">The boolean comparator.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="value"/> OR <paramref name="comparison"/>
|
||||
/// evaluate to <see langword="true"/>, or <see langword="false"/> otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if <paramref name="value" /> OR <paramref name="comparison" />
|
||||
/// evaluate to <see langword="true" />, or <see langword="false" /> otherwise.
|
||||
/// </returns>
|
||||
public static bool Or(this bool value, bool comparison)
|
||||
{
|
||||
return value || comparison;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the value of this boolean as represented by <see cref="byte"/>.
|
||||
/// Gets the value of this boolean as represented by <see cref="byte" />.
|
||||
/// </summary>
|
||||
/// <param name="value">The boolean.</param>
|
||||
/// <returns>Returns 1 if <paramref name="value"/> is <see langword="true"/>, or 0 otherwise.</returns>
|
||||
/// <returns>Returns 1 if <paramref name="value" /> is <see langword="true" />, or 0 otherwise.</returns>
|
||||
public static byte ToByte(this bool value)
|
||||
{
|
||||
return (byte)value.ToInt32();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the value of this boolean as represented by <see cref="short"/>.
|
||||
/// Gets the value of this boolean as represented by <see cref="short" />.
|
||||
/// </summary>
|
||||
/// <param name="value">The boolean.</param>
|
||||
/// <returns>Returns 1 if <paramref name="value"/> is <see langword="true"/>, or 0 otherwise.</returns>
|
||||
/// <returns>Returns 1 if <paramref name="value" /> is <see langword="true" />, or 0 otherwise.</returns>
|
||||
public static short ToInt16(this bool value)
|
||||
{
|
||||
return (short)value.ToInt32();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the value of this boolean as represented by <see cref="int"/>.
|
||||
/// Gets the value of this boolean as represented by <see cref="int" />.
|
||||
/// </summary>
|
||||
/// <param name="value">The boolean.</param>
|
||||
/// <returns>Returns 1 if <paramref name="value"/> is <see langword="true"/>, or 0 otherwise.</returns>
|
||||
/// <returns>Returns 1 if <paramref name="value" /> is <see langword="true" />, or 0 otherwise.</returns>
|
||||
public static int ToInt32(this bool value)
|
||||
{
|
||||
return value ? 1 : 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the value of this boolean as represented by <see cref="long"/>.
|
||||
/// Gets the value of this boolean as represented by <see cref="long" />.
|
||||
/// </summary>
|
||||
/// <param name="value">The boolean.</param>
|
||||
/// <returns>Returns 1 if <paramref name="value"/> is <see langword="true"/>, 0 otherwise.</returns>
|
||||
/// <returns>Returns 1 if <paramref name="value" /> is <see langword="true" />, 0 otherwise.</returns>
|
||||
public static long ToInt64(this bool value)
|
||||
{
|
||||
return value.ToInt32();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs logical XNOR on this <see cref="bool"/> and another <see cref="bool"/>.
|
||||
/// Performs logical XNOR on this <see cref="bool" /> and another <see cref="bool" />.
|
||||
/// </summary>
|
||||
/// <param name="value">The boolean.</param>
|
||||
/// <param name="comparison">The boolean comparator.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="value"/> XNOR <paramref name="comparison"/>
|
||||
/// evaluate to <see langword="true"/>, or <see langword="false"/> otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if <paramref name="value" /> XNOR <paramref name="comparison" />
|
||||
/// evaluate to <see langword="true" />, or <see langword="false" /> otherwise.
|
||||
/// </returns>
|
||||
public static bool XNOr(this bool value, bool comparison)
|
||||
{
|
||||
return !(value ^ comparison);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs logical XOR on this <see cref="bool"/> and another <see cref="bool"/>.
|
||||
/// Performs logical XOR on this <see cref="bool" /> and another <see cref="bool" />.
|
||||
/// </summary>
|
||||
/// <param name="value">The boolean.</param>
|
||||
/// <param name="comparison">The boolean comparator.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="value"/> XOR <paramref name="comparison"/>
|
||||
/// evaluate to <see langword="true"/>, or <see langword="false"/> otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if <paramref name="value" /> XOR <paramref name="comparison" />
|
||||
/// evaluate to <see langword="true" />, or <see langword="false" /> otherwise.
|
||||
/// </returns>
|
||||
public static bool XOr(this bool value, bool comparison)
|
||||
{
|
||||
return value ^ comparison;
|
||||
|
@ -6,101 +6,68 @@
|
||||
using System.Text;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="byte"/>.
|
||||
/// Extension methods for <see cref="byte" />.
|
||||
/// </summary>
|
||||
public static class ByteExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a <see cref="string"/> literally representing the raw values in the <see cref="byte"/>[].
|
||||
/// Gets a <see cref="string" /> literally representing the raw values in the <see cref="byte" />[].
|
||||
/// </summary>
|
||||
/// <param name="bytes">The bytes to get.</param>
|
||||
/// <returns>Returns a <see cref="string"/>.</returns>
|
||||
/// <returns>Returns a <see cref="string" />.</returns>
|
||||
public static string AsString(this IEnumerable<byte> bytes)
|
||||
{
|
||||
return BitConverter.ToString(bytes.ToArray());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="byte"/>[] to an <see cref="short"/>.
|
||||
/// Converts the <see cref="byte" />[] to an <see cref="short" />.
|
||||
/// </summary>
|
||||
/// <param name="bytes">The bytes to convert.</param>
|
||||
/// <returns>Returns an <see cref="short"/>.</returns>
|
||||
/// <returns>Returns an <see cref="short" />.</returns>
|
||||
public static short GetInt16(this IEnumerable<byte> bytes)
|
||||
{
|
||||
return BitConverter.ToInt16(bytes.ToArray(), 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="byte"/>[] to an <see cref="int"/>.
|
||||
/// Converts the <see cref="byte" />[] to an <see cref="int" />.
|
||||
/// </summary>
|
||||
/// <param name="bytes">The bytes to convert.</param>
|
||||
/// <returns>Returns an <see cref="int"/>.</returns>
|
||||
/// <returns>Returns an <see cref="int" />.</returns>
|
||||
public static int GetInt32(this IEnumerable<byte> bytes)
|
||||
{
|
||||
return BitConverter.ToInt32(bytes.ToArray(), 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="byte"/>[] to an <see cref="long"/>.
|
||||
/// Converts the <see cref="byte" />[] to an <see cref="long" />.
|
||||
/// </summary>
|
||||
/// <param name="bytes">The bytes to convert.</param>
|
||||
/// <returns>Returns an <see cref="long"/>.</returns>
|
||||
/// <returns>Returns an <see cref="long" />.</returns>
|
||||
public static long GetInt64(this IEnumerable<byte> bytes)
|
||||
{
|
||||
return BitConverter.ToInt64(bytes.ToArray(), 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="byte"/>[] to a <see cref="ushort"/>.
|
||||
/// Gets a <see cref="string" /> representing the value the <see cref="byte" />[] with
|
||||
/// <see cref="Encoding.UTF8" /> encoding.
|
||||
/// </summary>
|
||||
/// <param name="bytes">The bytes to convert.</param>
|
||||
/// <returns>Returns an <see cref="ushort"/>.</returns>
|
||||
[CLSCompliant(false)]
|
||||
public static ushort GetUInt16(this IEnumerable<byte> bytes)
|
||||
{
|
||||
return BitConverter.ToUInt16(bytes.ToArray(), 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="byte"/>[] to an <see cref="uint"/>.
|
||||
/// </summary>
|
||||
/// <param name="bytes">The bytes to convert.</param>
|
||||
/// <returns>Returns an <see cref="uint"/>.</returns>
|
||||
[CLSCompliant(false)]
|
||||
public static uint GetUInt32(this IEnumerable<byte> bytes)
|
||||
{
|
||||
return BitConverter.ToUInt32(bytes.ToArray(), 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="byte"/>[] to an <see cref="ulong"/>.
|
||||
/// </summary>
|
||||
/// <param name="bytes">The bytes to convert.</param>
|
||||
/// <returns>Returns an <see cref="ulong"/>.</returns>
|
||||
[CLSCompliant(false)]
|
||||
public static ulong GetUInt64(this IEnumerable<byte> bytes)
|
||||
{
|
||||
return BitConverter.ToUInt64(bytes.ToArray(), 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="string"/> representing the value the <see cref="byte"/>[] with
|
||||
/// <see cref="Encoding.UTF8"/> encoding.
|
||||
/// </summary>
|
||||
/// <param name="bytes">The bytes to convert.</param>
|
||||
/// <returns>Returns a <see cref="string"/>.</returns>
|
||||
/// <returns>Returns a <see cref="string" />.</returns>
|
||||
public static string GetString(this IEnumerable<byte> bytes)
|
||||
{
|
||||
return bytes.GetString(Encoding.UTF8);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="string"/> representing the value the <see cref="byte"/>[] with the provided encoding.
|
||||
/// Gets a <see cref="string" /> representing the value the <see cref="byte" />[] with the provided encoding.
|
||||
/// </summary>
|
||||
/// <param name="bytes">The bytes to convert.</param>
|
||||
/// <param name="encoding">The encoding to use.</param>
|
||||
/// <returns>Returns a <see cref="string"/>.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="encoding"/> is <see langword="null"/>.</exception>
|
||||
/// <returns>Returns a <see cref="string" />.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="encoding" /> is <see langword="null" />.</exception>
|
||||
public static string GetString(this IEnumerable<byte> bytes, Encoding encoding)
|
||||
{
|
||||
if (encoding is null)
|
||||
@ -112,5 +79,38 @@
|
||||
var array = bytes.ToArray();
|
||||
return encoding.GetString(array, 0, array.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="byte" />[] to a <see cref="ushort" />.
|
||||
/// </summary>
|
||||
/// <param name="bytes">The bytes to convert.</param>
|
||||
/// <returns>Returns an <see cref="ushort" />.</returns>
|
||||
[CLSCompliant(false)]
|
||||
public static ushort GetUInt16(this IEnumerable<byte> bytes)
|
||||
{
|
||||
return BitConverter.ToUInt16(bytes.ToArray(), 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="byte" />[] to an <see cref="uint" />.
|
||||
/// </summary>
|
||||
/// <param name="bytes">The bytes to convert.</param>
|
||||
/// <returns>Returns an <see cref="uint" />.</returns>
|
||||
[CLSCompliant(false)]
|
||||
public static uint GetUInt32(this IEnumerable<byte> bytes)
|
||||
{
|
||||
return BitConverter.ToUInt32(bytes.ToArray(), 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="byte" />[] to an <see cref="ulong" />.
|
||||
/// </summary>
|
||||
/// <param name="bytes">The bytes to convert.</param>
|
||||
/// <returns>Returns an <see cref="ulong" />.</returns>
|
||||
[CLSCompliant(false)]
|
||||
public static ulong GetUInt64(this IEnumerable<byte> bytes)
|
||||
{
|
||||
return BitConverter.ToUInt64(bytes.ToArray(), 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,29 +6,29 @@
|
||||
using System.Text;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="char"/>.
|
||||
/// Extension methods for <see cref="char" />.
|
||||
/// </summary>
|
||||
public static class CharExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Generates a new random string by filling it with characters found in <see cref="chars"/>.
|
||||
/// Generates a new random string by filling it with characters found in <see cref="chars" />.
|
||||
/// </summary>
|
||||
/// <param name="chars">The character set.</param>
|
||||
/// <param name="length">The length of the string to generate.</param>
|
||||
/// <returns>Returns a <see cref="string"/> containing <paramref name="length"/> characters.</returns>
|
||||
/// <returns>Returns a <see cref="string" /> containing <paramref name="length" /> characters.</returns>
|
||||
public static string Random(this char[] chars, int length)
|
||||
{
|
||||
return chars.Random(length, RandomExtensions.Random);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a new random string by filling it with characters found in <see cref="chars"/>.
|
||||
/// Generates a new random string by filling it with characters found in <see cref="chars" />.
|
||||
/// </summary>
|
||||
/// <param name="chars">The character set.</param>
|
||||
/// <param name="length">The length of the string to generate.</param>
|
||||
/// <param name="random">The <see cref="System.Random"/> instance.</param>
|
||||
/// <returns>Returns a <see cref="string"/> containing <paramref name="length"/> characters.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="random"/> is <see langword="null"/>.</exception>
|
||||
/// <param name="random">The <see cref="System.Random" /> instance.</param>
|
||||
/// <returns>Returns a <see cref="string" /> containing <paramref name="length" /> characters.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
|
||||
public static string Random(this char[] chars, int length, Random random)
|
||||
{
|
||||
if (chars is null)
|
||||
@ -51,35 +51,37 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a new random string by filling it with characters found in <see cref="chars"/>.
|
||||
/// Generates a new random string by filling it with characters found in <see cref="chars" />.
|
||||
/// </summary>
|
||||
/// <param name="chars">The character set.</param>
|
||||
/// <param name="length">The length of the string to generate.</param>
|
||||
/// <returns>Returns a <see cref="string"/> containing <paramref name="length"/> characters.</returns>
|
||||
/// <returns>Returns a <see cref="string" /> containing <paramref name="length" /> characters.</returns>
|
||||
public static string Random(this IEnumerable<char> chars, int length)
|
||||
{
|
||||
return chars.Random(length, RandomExtensions.Random);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a new random string by filling it with characters found in <see cref="chars"/>.
|
||||
/// Generates a new random string by filling it with characters found in <see cref="chars" />.
|
||||
/// </summary>
|
||||
/// <param name="chars">The character set.</param>
|
||||
/// <param name="length">The length of the string to generate.</param>
|
||||
/// <param name="random">The <see cref="System.Random"/> instance.</param>
|
||||
/// <returns>Returns a <see cref="string"/> containing <paramref name="length"/> characters.</returns>
|
||||
/// <param name="random">The <see cref="System.Random" /> instance.</param>
|
||||
/// <returns>Returns a <see cref="string" /> containing <paramref name="length" /> characters.</returns>
|
||||
public static string Random(this IEnumerable<char> chars, int length, Random random)
|
||||
{
|
||||
return chars.ToArray().Random(length, random);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Repeats a character a specified number of times.
|
||||
/// Repeats a character a specified number of times.
|
||||
/// </summary>
|
||||
/// <param name="c">The character to repeat.</param>
|
||||
/// <param name="count">The repeat count.</param>
|
||||
/// <returns>Returns a <see cref="string"/> whose value is <paramref name="c"/> repeated
|
||||
/// <paramref name="count"/> times.</returns>
|
||||
/// <returns>
|
||||
/// Returns a <see cref="string" /> whose value is <paramref name="c" /> repeated
|
||||
/// <paramref name="count" /> times.
|
||||
/// </returns>
|
||||
public static string Repeat(this char c, int count)
|
||||
{
|
||||
return new string(c, count);
|
||||
|
@ -3,19 +3,21 @@
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="IComparable"/>.
|
||||
/// Extension methods for <see cref="IComparable" />.
|
||||
/// </summary>
|
||||
public static class ComparableExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Determines if <paramref name="actual"/> is between <paramref name="lower"/> and <paramref name="upper"/>.
|
||||
/// Determines if <paramref name="actual" /> is between <paramref name="lower" /> and <paramref name="upper" />.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The comparable type.</typeparam>
|
||||
/// <param name="actual">The value to compare.</param>
|
||||
/// <param name="lower">The exclusive lower bound.</param>
|
||||
/// <param name="upper">The exclusive upper bound.</param>
|
||||
/// <returns>Returns <see langword="true"/> if the value is between the bounds, <see langword="false"/>
|
||||
/// otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if the value is between the bounds, <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
public static bool Between<T>(this T actual, T lower, T upper)
|
||||
where T : IComparable<T>
|
||||
{
|
||||
|
@ -3,20 +3,22 @@
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="IConvertible"/>.
|
||||
/// Extension methods for <see cref="IConvertible" />.
|
||||
/// </summary>
|
||||
public static class ConvertibleExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts the object to another type.
|
||||
/// Converts the object to another type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to convert to.</typeparam>
|
||||
/// <param name="value">The object to convert.</param>
|
||||
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
|
||||
/// <returns>Returns the value converted to <see cref="T"/>.</returns>
|
||||
/// <exception cref="InvalidCastException">This conversion is not supported.
|
||||
/// -or-
|
||||
/// <paramref name="value"/> is <see langword="null"/> and <typeparamref name="T"/> is a value type.</exception>
|
||||
/// <returns>Returns the value converted to <see cref="T" />.</returns>
|
||||
/// <exception cref="InvalidCastException">
|
||||
/// This conversion is not supported.
|
||||
/// -or-
|
||||
/// <paramref name="value" /> is <see langword="null" /> and <typeparamref name="T" /> is a value type.
|
||||
/// </exception>
|
||||
[CLSCompliant(false)]
|
||||
public static T To<T>(this IConvertible value, IFormatProvider provider = null)
|
||||
{
|
||||
@ -29,12 +31,12 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the object to another type, returning the default value on failure.
|
||||
/// Converts the object to another type, returning the default value on failure.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to convert to.</typeparam>
|
||||
/// <param name="value">The object to convert.</param>
|
||||
/// <param name="provider">The format provider.</param>
|
||||
/// <returns>Returns the value converted to <see cref="T"/>.</returns>
|
||||
/// <returns>Returns the value converted to <see cref="T" />.</returns>
|
||||
/// <exception cref="InvalidCastException">This conversion is not supported.</exception>
|
||||
[CLSCompliant(false)]
|
||||
public static T ToOrDefault<T>(this IConvertible value, IFormatProvider provider = null)
|
||||
@ -43,13 +45,13 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the object to another type, returning the default value on failure.
|
||||
/// Converts the object to another type, returning the default value on failure.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to convert to.</typeparam>
|
||||
/// <param name="value">The object to convert.</param>
|
||||
/// <param name="newObj">The parameter where the result should be sent.</param>
|
||||
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
|
||||
/// <returns>Returns <see langword="true"/> on success, <see langword="true"/> on failure.</returns>
|
||||
/// <returns>Returns <see langword="true" /> on success, <see langword="true" /> on failure.</returns>
|
||||
[CLSCompliant(false)]
|
||||
public static bool ToOrDefault<T>(this IConvertible value, out T newObj, IFormatProvider provider = null)
|
||||
{
|
||||
@ -72,13 +74,42 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the object to another type, returning a different value on failure.
|
||||
/// Converts the object to another type, returning <see langword="null" /> on failure.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to convert to.</typeparam>
|
||||
/// <param name="value">The object to convert.</param>
|
||||
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
|
||||
/// <returns>Returns a <see cref="T" /> or <see langword="null" />.</returns>
|
||||
[CLSCompliant(false)]
|
||||
public static T ToOrNull<T>(this IConvertible value, IFormatProvider provider = null)
|
||||
where T : class
|
||||
{
|
||||
return value.ToOrNull(out T v, provider) ? v : null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the object to another type, returning <see langword="null" /> on failure.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to convert to.</typeparam>
|
||||
/// <param name="value">The object to convert.</param>
|
||||
/// <param name="newObj">The parameter where the result should be sent.</param>
|
||||
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
|
||||
/// <returns>Returns a <see cref="T" /> or <see langword="null" />.</returns>
|
||||
[CLSCompliant(false)]
|
||||
public static bool ToOrNull<T>(this IConvertible value, out T newObj, IFormatProvider provider = null)
|
||||
where T : class
|
||||
{
|
||||
return ToOrOther(value, out newObj, null, provider);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the object to another type, returning a different value on failure.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to convert to.</typeparam>
|
||||
/// <param name="value">The object to convert.</param>
|
||||
/// <param name="other">The backup value.</param>
|
||||
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
|
||||
/// <returns>Returns the value converted to <see cref="T"/>.</returns>
|
||||
/// <returns>Returns the value converted to <see cref="T" />.</returns>
|
||||
[CLSCompliant(false)]
|
||||
public static T ToOrOther<T>(this IConvertible value, T other, IFormatProvider provider = null)
|
||||
{
|
||||
@ -98,14 +129,14 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the object to another type, returning a different value on failure.
|
||||
/// Converts the object to another type, returning a different value on failure.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to convert to.</typeparam>
|
||||
/// <param name="value">The object to convert.</param>
|
||||
/// <param name="newObj">The parameter where the result should be sent.</param>
|
||||
/// <param name="other">The backup value.</param>
|
||||
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
|
||||
/// <returns>Returns <see langword="true"/> on success, <see langword="true"/> on failure.</returns>
|
||||
/// <returns>Returns <see langword="true" /> on success, <see langword="true" /> on failure.</returns>
|
||||
[CLSCompliant(false)]
|
||||
public static bool ToOrOther<T>(this IConvertible value, out T newObj, T other, IFormatProvider provider = null)
|
||||
{
|
||||
@ -126,34 +157,5 @@
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the object to another type, returning <see langword="null"/> on failure.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to convert to.</typeparam>
|
||||
/// <param name="value">The object to convert.</param>
|
||||
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
|
||||
/// <returns>Returns a <see cref="T"/> or <see langword="null"/>.</returns>
|
||||
[CLSCompliant(false)]
|
||||
public static T ToOrNull<T>(this IConvertible value, IFormatProvider provider = null)
|
||||
where T : class
|
||||
{
|
||||
return value.ToOrNull(out T v, provider) ? v : null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the object to another type, returning <see langword="null"/> on failure.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to convert to.</typeparam>
|
||||
/// <param name="value">The object to convert.</param>
|
||||
/// <param name="newObj">The parameter where the result should be sent.</param>
|
||||
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
|
||||
/// <returns>Returns a <see cref="T"/> or <see langword="null"/>.</returns>
|
||||
[CLSCompliant(false)]
|
||||
public static bool ToOrNull<T>(this IConvertible value, out T newObj, IFormatProvider provider = null)
|
||||
where T : class
|
||||
{
|
||||
return ToOrOther(value, out newObj, null, provider);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,41 +3,43 @@
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="DateTime"/>.
|
||||
/// Extension methods for <see cref="DateTime" />.
|
||||
/// </summary>
|
||||
public static class DateTimeExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns a rounded integer of the number of years since a given date as of today.
|
||||
/// Returns a rounded integer of the number of years since a given date as of today.
|
||||
/// </summary>
|
||||
/// <param name="date">The date from which to start.</param>
|
||||
/// <returns>Returns the number of years since <paramref name="date"/> as of today.</returns>
|
||||
/// <returns>Returns the number of years since <paramref name="date" /> as of today.</returns>
|
||||
public static int Age(this DateTime date)
|
||||
{
|
||||
return date.Age(DateTime.Today);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a rounded integer of the number of years since a given date as of another given date.
|
||||
/// Returns a rounded integer of the number of years since a given date as of another given date.
|
||||
/// </summary>
|
||||
/// <param name="date">The date from which to start.</param>
|
||||
/// <param name="asOf">The date at which to stop counting.</param>
|
||||
/// <returns>Returns the integer number of years since <paramref name="date"/> as of
|
||||
/// <paramref name="asOf"/>.</returns>
|
||||
/// <returns>
|
||||
/// Returns the integer number of years since <paramref name="date" /> as of
|
||||
/// <paramref name="asOf" />.
|
||||
/// </returns>
|
||||
public static int Age(this DateTime date, DateTime asOf)
|
||||
{
|
||||
return (int)(((asOf.Date - TimeSpan.FromDays(1) - date.Date).TotalDays + 1) / 365.2425);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a DateTime representing the first occurence of a specified day in the current month.
|
||||
/// Gets a DateTime representing the first occurence of a specified day in the current month.
|
||||
/// </summary>
|
||||
/// <param name="current">The current day.</param>
|
||||
/// <param name="dayOfWeek">The current day of week.</param>
|
||||
/// <returns>Returns a date representing the first occurence of <paramref name="dayOfWeek"/>.</returns>
|
||||
/// <returns>Returns a date representing the first occurence of <paramref name="dayOfWeek" />.</returns>
|
||||
public static DateTime First(this DateTime current, DayOfWeek dayOfWeek)
|
||||
{
|
||||
DateTime first = current.FirstDayOfMonth();
|
||||
var first = current.FirstDayOfMonth();
|
||||
|
||||
if (first.DayOfWeek != dayOfWeek)
|
||||
{
|
||||
@ -48,7 +50,7 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="DateTime"/> representing the first day in the current month.
|
||||
/// Gets a <see cref="DateTime" /> representing the first day in the current month.
|
||||
/// </summary>
|
||||
/// <param name="current">The current date.</param>
|
||||
/// <returns>Returns a date representing the first day of the month>.</returns>
|
||||
@ -58,7 +60,24 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="DateTime"/> representing the last day in the current month.
|
||||
/// Gets a <see cref="DateTime" /> representing the last specified day in the current month.
|
||||
/// </summary>
|
||||
/// <param name="current">The current date.</param>
|
||||
/// <param name="dayOfWeek">The current day of week.</param>
|
||||
/// <returns>Returns a date representing the final occurence of <paramref name="dayOfWeek" />.</returns>
|
||||
public static DateTime Last(this DateTime current, DayOfWeek dayOfWeek)
|
||||
{
|
||||
var last = current.LastDayOfMonth();
|
||||
var lastDayOfWeek = last.DayOfWeek;
|
||||
|
||||
var diff = dayOfWeek - lastDayOfWeek;
|
||||
var offset = diff > 0 ? diff - 7 : diff;
|
||||
|
||||
return last.AddDays(offset);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="DateTime" /> representing the last day in the current month.
|
||||
/// </summary>
|
||||
/// <param name="current">The current date.</param>
|
||||
/// <returns>Returns a date representing the last day of the month>.</returns>
|
||||
@ -69,29 +88,12 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="DateTime"/> representing the last specified day in the current month.
|
||||
/// </summary>
|
||||
/// <param name="current">The current date.</param>
|
||||
/// <param name="dayOfWeek">The current day of week.</param>
|
||||
/// <returns>Returns a date representing the final occurence of <paramref name="dayOfWeek"/>.</returns>
|
||||
public static DateTime Last(this DateTime current, DayOfWeek dayOfWeek)
|
||||
{
|
||||
DateTime last = current.LastDayOfMonth();
|
||||
DayOfWeek lastDayOfWeek = last.DayOfWeek;
|
||||
|
||||
var diff = dayOfWeek - lastDayOfWeek;
|
||||
var offset = diff > 0 ? diff - 7 : diff;
|
||||
|
||||
return last.AddDays(offset);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="DateTime"/> representing the first date following the current date which falls on the
|
||||
/// given day of the week.
|
||||
/// Gets a <see cref="DateTime" /> representing the first date following the current date which falls on the
|
||||
/// given day of the week.
|
||||
/// </summary>
|
||||
/// <param name="current">The current date.</param>
|
||||
/// <param name="dayOfWeek">The day of week for the next date to get.</param>
|
||||
/// <returns>Returns a date representing the next occurence of <paramref name="dayOfWeek"/>.</returns>
|
||||
/// <returns>Returns a date representing the next occurence of <paramref name="dayOfWeek" />.</returns>
|
||||
public static DateTime Next(this DateTime current, DayOfWeek dayOfWeek)
|
||||
{
|
||||
var offsetDays = dayOfWeek - current.DayOfWeek;
|
||||
@ -105,12 +107,14 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="DateTime"/> to a Unix timestamp.
|
||||
/// Converts the <see cref="DateTime" /> to a Unix timestamp.
|
||||
/// </summary>
|
||||
/// <param name="time">The <see cref="DateTime"/> instance.</param>
|
||||
/// <param name="useMillis">Optional. Whether or not the return value should be represented as milliseconds.
|
||||
/// Defaults to <see langword="false"/>.</param>
|
||||
/// <returns>Returns a Unix timestamp representing the provided <see cref="DateTime"/>.</returns>
|
||||
/// <param name="time">The <see cref="DateTime" /> instance.</param>
|
||||
/// <param name="useMillis">
|
||||
/// Optional. Whether or not the return value should be represented as milliseconds.
|
||||
/// Defaults to <see langword="false" />.
|
||||
/// </param>
|
||||
/// <returns>Returns a Unix timestamp representing the provided <see cref="DateTime" />.</returns>
|
||||
public static long ToUnixTimeStamp(this DateTime time, bool useMillis = false)
|
||||
{
|
||||
DateTimeOffset offset = time;
|
||||
|
@ -6,17 +6,17 @@
|
||||
using System.Web;
|
||||
|
||||
/// <summary>
|
||||
/// A set of extension methods for <see cref="Dictionary{TKey,TValue}"/>.
|
||||
/// A set of extension methods for <see cref="Dictionary{TKey,TValue}" />.
|
||||
/// </summary>
|
||||
public static class DictionaryExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a <see cref="IReadOnlyDictionary{T1,T2}"/> to an object-relational-safe connection string.
|
||||
/// Converts a <see cref="IReadOnlyDictionary{T1,T2}" /> to an object-relational-safe connection string.
|
||||
/// </summary>
|
||||
/// <typeparam name="T1">The key type.</typeparam>
|
||||
/// <typeparam name="T2">The value type.</typeparam>
|
||||
/// <param name="dictionary">The dictionary.</param>
|
||||
/// <returns>Returns a <see cref="string"/> representing the dictionary as a key=value; set.</returns>
|
||||
/// <returns>Returns a <see cref="string" /> representing the dictionary as a key=value; set.</returns>
|
||||
public static string ToConnectionString<T1, T2>(this IReadOnlyDictionary<T1, T2> dictionary)
|
||||
{
|
||||
static string SanitizeValue<T>(T value)
|
||||
@ -27,41 +27,41 @@
|
||||
: value.ToString();
|
||||
}
|
||||
|
||||
IEnumerable<string> strings = dictionary.Select(o => $"{o.Key}={SanitizeValue(o.Value)}");
|
||||
var strings = dictionary.Select(o => $"{o.Key}={SanitizeValue(o.Value)}");
|
||||
return string.Join(";", strings);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="IDictionary{T1,T2}"/> to an object-relational-safe connection string.
|
||||
/// Converts a <see cref="IDictionary{T1,T2}" /> to an object-relational-safe connection string.
|
||||
/// </summary>
|
||||
/// <typeparam name="T1">The key type.</typeparam>
|
||||
/// <typeparam name="T2">The value type.</typeparam>
|
||||
/// <param name="dictionary">The dictionary.</param>
|
||||
/// <returns>Returns a <see cref="string"/> representing the dictionary as a key=value; set.</returns>
|
||||
/// <returns>Returns a <see cref="string" /> representing the dictionary as a key=value; set.</returns>
|
||||
public static string ToConnectionString<T1, T2>(this IDictionary<T1, T2> dictionary)
|
||||
{
|
||||
return ((IReadOnlyDictionary<T1, T2>)dictionary).ToConnectionString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="Dictionary{T1,T2}"/> to an object-relational-safe connection string.
|
||||
/// Converts a <see cref="Dictionary{T1,T2}" /> to an object-relational-safe connection string.
|
||||
/// </summary>
|
||||
/// <typeparam name="T1">The key type.</typeparam>
|
||||
/// <typeparam name="T2">The value type.</typeparam>
|
||||
/// <param name="dictionary">The dictionary.</param>
|
||||
/// <returns>Returns a <see cref="string"/> representing the dictionary as a key=value; set.</returns>
|
||||
/// <returns>Returns a <see cref="string" /> representing the dictionary as a key=value; set.</returns>
|
||||
public static string ToConnectionString<T1, T2>(this Dictionary<T1, T2> dictionary)
|
||||
{
|
||||
return ((IReadOnlyDictionary<T1, T2>)dictionary).ToConnectionString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts an <see cref="IReadOnlyDictionary{T1,T2}"/> to a HTTP GET parameter string.
|
||||
/// Converts an <see cref="IReadOnlyDictionary{T1,T2}" /> to a HTTP GET parameter string.
|
||||
/// </summary>
|
||||
/// <typeparam name="T1">The key type.</typeparam>
|
||||
/// <typeparam name="T2">The value type.</typeparam>
|
||||
/// <param name="dictionary">The dictionary.</param>
|
||||
/// <returns>Returns a <see cref="string"/> representing the dictionary as a key=value& set.</returns>
|
||||
/// <returns>Returns a <see cref="string" /> representing the dictionary as a key=value& set.</returns>
|
||||
public static string ToGetParameters<T1, T2>(this IReadOnlyDictionary<T1, T2> dictionary)
|
||||
{
|
||||
static string Sanitize(KeyValuePair<T1, T2> kvp)
|
||||
@ -75,24 +75,24 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts an <see cref="IDictionary{T1,T2}"/> to a HTTP GET parameter string.
|
||||
/// Converts an <see cref="IDictionary{T1,T2}" /> to a HTTP GET parameter string.
|
||||
/// </summary>
|
||||
/// <typeparam name="T1">The key type.</typeparam>
|
||||
/// <typeparam name="T2">The value type.</typeparam>
|
||||
/// <param name="dictionary">The dictionary.</param>
|
||||
/// <returns>Returns a <see cref="string"/> representing the dictionary as a key=value& set.</returns>
|
||||
/// <returns>Returns a <see cref="string" /> representing the dictionary as a key=value& set.</returns>
|
||||
public static string ToGetParameters<T1, T2>(this IDictionary<T1, T2> dictionary)
|
||||
{
|
||||
return ((IReadOnlyDictionary<T1, T2>)dictionary).ToGetParameters();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="Dictionary{T1,T2}"/> to a HTTP GET parameter string.
|
||||
/// Converts a <see cref="Dictionary{T1,T2}" /> to a HTTP GET parameter string.
|
||||
/// </summary>
|
||||
/// <typeparam name="T1">The key type.</typeparam>
|
||||
/// <typeparam name="T2">The value type.</typeparam>
|
||||
/// <param name="dictionary">The dictionary.</param>
|
||||
/// <returns>Returns a <see cref="string"/> representing the dictionary as a key=value& set.</returns>
|
||||
/// <returns>Returns a <see cref="string" /> representing the dictionary as a key=value& set.</returns>
|
||||
public static string ToGetParameters<T1, T2>(this Dictionary<T1, T2> dictionary)
|
||||
{
|
||||
return ((IReadOnlyDictionary<T1, T2>)dictionary).ToGetParameters();
|
||||
|
@ -3,83 +3,89 @@
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="double"/>.
|
||||
/// Extension methods for <see cref="double" />.
|
||||
/// </summary>
|
||||
public static class DoubleExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Clamps a value between a minimum and a maximum value.
|
||||
/// Clamps a value between a minimum and a maximum value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to clamp.</param>
|
||||
/// <param name="min">The minimum value.</param>
|
||||
/// <param name="max">The maximum value.</param>
|
||||
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
|
||||
/// <paramref name="min"/> if <paramref name="value"/> is less than it,
|
||||
/// or <paramref name="value"/> itself otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <paramref name="max" /> if <paramref name="value" /> is greater than it,
|
||||
/// <paramref name="min" /> if <paramref name="value" /> is less than it,
|
||||
/// or <paramref name="value" /> itself otherwise.
|
||||
/// </returns>
|
||||
public static double Clamp(this double value, double min, double max)
|
||||
{
|
||||
return Math.Min(Math.Max(value, min), max);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts an angle from degrees to radians.
|
||||
/// Converts an angle from degrees to radians.
|
||||
/// </summary>
|
||||
/// <param name="angle">The angle in degrees.</param>
|
||||
/// <returns>Returns <paramref name="angle"/> in radians.</returns>
|
||||
/// <returns>Returns <paramref name="angle" /> in radians.</returns>
|
||||
public static double DegreesToRadians(this double angle)
|
||||
{
|
||||
return Math.PI * angle / 180.0;
|
||||
return (Math.PI * angle) / 180.0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="double"/> to a <see cref="byte"/>[].
|
||||
/// Converts the <see cref="double" /> to a <see cref="byte" />[].
|
||||
/// </summary>
|
||||
/// <param name="number">The number to convert.</param>
|
||||
/// <returns>Returns a <see cref="byte"/>[].</returns>
|
||||
/// <returns>Returns a <see cref="byte" />[].</returns>
|
||||
public static byte[] GetBytes(this double number)
|
||||
{
|
||||
return BitConverter.GetBytes(number);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the <see cref="double"/> is even.
|
||||
/// Determines if the <see cref="double" /> is even.
|
||||
/// </summary>
|
||||
/// <param name="number">The number.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is even, <see langword="false"/>
|
||||
/// otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if <paramref name="number" /> is even, <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
public static bool IsEven(this double number)
|
||||
{
|
||||
return Math.Abs(number % 2.0) < double.Epsilon;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the <see cref="double"/> is odd.
|
||||
/// Determines if the <see cref="double" /> is odd.
|
||||
/// </summary>
|
||||
/// <param name="number">The number.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is odd, <see langword="false"/>
|
||||
/// otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if <paramref name="number" /> is odd, <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
public static bool IsOdd(this double number)
|
||||
{
|
||||
return !number.IsEven();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts an angle from radians to degrees.
|
||||
/// Converts an angle from radians to degrees.
|
||||
/// </summary>
|
||||
/// <param name="angle">The angle in radians.</param>
|
||||
/// <returns>Returns <paramref name="angle"/> in degrees.</returns>
|
||||
/// <returns>Returns <paramref name="angle" /> in degrees.</returns>
|
||||
public static double RadiansToDegrees(this double angle)
|
||||
{
|
||||
return angle * (180.0 / Math.PI);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rounds to the nearest value.
|
||||
/// Rounds to the nearest value.
|
||||
/// </summary>
|
||||
/// <param name="v">The value to round.</param>
|
||||
/// <param name="nearest">The nearest value.</param>
|
||||
/// <returns>Returns the rounded value.</returns>
|
||||
public static double Round(this double v, int nearest = 1)
|
||||
public static double Round(this double v, double nearest = 1)
|
||||
{
|
||||
return Math.Round(v / nearest) * nearest;
|
||||
}
|
||||
|
@ -3,38 +3,40 @@
|
||||
using System.Net;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="EndPoint"/> and derived types.
|
||||
/// Extension methods for <see cref="EndPoint" /> and derived types.
|
||||
/// </summary>
|
||||
public static class EndPointExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the endpoint hostname.
|
||||
/// Gets the endpoint hostname.
|
||||
/// </summary>
|
||||
/// <param name="endPoint">The endpoint whose hostname to get.</param>
|
||||
/// <returns>Returns a <see cref="string"/> representing the hostname, which may be an IP or a DNS, or empty
|
||||
/// string on failure.</returns>
|
||||
/// <returns>
|
||||
/// Returns a <see cref="string" /> representing the hostname, which may be an IP or a DNS, or empty
|
||||
/// string on failure.
|
||||
/// </returns>
|
||||
public static string GetHost(this EndPoint endPoint)
|
||||
{
|
||||
return endPoint switch
|
||||
{
|
||||
IPEndPoint ip => ip.Address.ToString(),
|
||||
DnsEndPoint dns => dns.Host,
|
||||
_ => string.Empty,
|
||||
var _ => string.Empty,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the endpoint port.
|
||||
/// Gets the endpoint port.
|
||||
/// </summary>
|
||||
/// <param name="endPoint">The endpoint whose port to get.</param>
|
||||
/// <returns>Returns an <see cref="int"/> representing the port, or 0 on failure.</returns>
|
||||
/// <returns>Returns an <see cref="int" /> representing the port, or 0 on failure.</returns>
|
||||
public static int GetPort(this EndPoint endPoint)
|
||||
{
|
||||
return endPoint switch
|
||||
{
|
||||
IPEndPoint ip => ip.Port,
|
||||
DnsEndPoint dns => dns.Port,
|
||||
_ => 0,
|
||||
var _ => 0,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -4,21 +4,23 @@
|
||||
using System.Linq;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="IEnumerable{T}"/>.
|
||||
/// Extension methods for <see cref="IEnumerable{T}" />.
|
||||
/// </summary>
|
||||
public static class EnumerableExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Splits <paramref name="value"/> into chunks of size <paramref name="chunkSize"/>.
|
||||
/// Splits <paramref name="value" /> into chunks of size <paramref name="chunkSize" />.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Any type.</typeparam>
|
||||
/// <param name="value">The collection to split.</param>
|
||||
/// <param name="chunkSize">The maximum length of the nested <see cref="byte"/> collection.</param>
|
||||
/// <returns>Returns an <see cref="IEnumerable{T}"/> of <see cref="IEnumerable{T}"/> of <see cref="byte"/>
|
||||
/// values.</returns>
|
||||
/// <param name="chunkSize">The maximum length of the nested <see cref="byte" /> collection.</param>
|
||||
/// <returns>
|
||||
/// Returns an <see cref="IEnumerable{T}" /> of <see cref="IEnumerable{T}" /> of <see cref="byte" />
|
||||
/// values.
|
||||
/// </returns>
|
||||
public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> value, int chunkSize)
|
||||
{
|
||||
T[] enumerable = value.ToArray();
|
||||
var enumerable = value.ToArray();
|
||||
var count = enumerable.LongCount();
|
||||
chunkSize = chunkSize.Clamp(1, enumerable.Length);
|
||||
|
||||
|
@ -3,33 +3,37 @@
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="short"/>.
|
||||
/// Extension methods for <see cref="short" />.
|
||||
/// </summary>
|
||||
public static class Int16Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Clamps a value between a minimum and a maximum value.
|
||||
/// Clamps a value between a minimum and a maximum value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to clamp.</param>
|
||||
/// <param name="min">The minimum value.</param>
|
||||
/// <param name="max">The maximum value.</param>
|
||||
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
|
||||
/// <paramref name="min"/> if <paramref name="value"/> is less than it,
|
||||
/// or <paramref name="value"/> itself otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <paramref name="max" /> if <paramref name="value" /> is greater than it,
|
||||
/// <paramref name="min" /> if <paramref name="value" /> is less than it,
|
||||
/// or <paramref name="value" /> itself otherwise.
|
||||
/// </returns>
|
||||
public static short Clamp(this short value, short min, short max)
|
||||
{
|
||||
return Math.Min(Math.Max(value, min), max);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clamps a value between a minimum and a maximum value.
|
||||
/// Clamps a value between a minimum and a maximum value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to clamp.</param>
|
||||
/// <param name="min">The minimum value.</param>
|
||||
/// <param name="max">The maximum value.</param>
|
||||
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
|
||||
/// <paramref name="min"/> if <paramref name="value"/> is less than it,
|
||||
/// or <paramref name="value"/> itself otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <paramref name="max" /> if <paramref name="value" /> is greater than it,
|
||||
/// <paramref name="min" /> if <paramref name="value" /> is less than it,
|
||||
/// or <paramref name="value" /> itself otherwise.
|
||||
/// </returns>
|
||||
[CLSCompliant(false)]
|
||||
public static ushort Clamp(this ushort value, ushort min, ushort max)
|
||||
{
|
||||
@ -37,23 +41,27 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="short"/> to a <see cref="DateTime"/> treating it as a Unix timestamp.
|
||||
/// Converts the <see cref="short" /> to a <see cref="DateTime" /> treating it as a Unix timestamp.
|
||||
/// </summary>
|
||||
/// <param name="timestamp">The timestamp.</param>
|
||||
/// <param name="isMillis">Optional. Whether or not the input value should be treated as milliseconds. Defaults
|
||||
/// to <see langword="false"/>..</param>
|
||||
/// <returns>Returns a <see cref="DateTime"/> representing <paramref name="timestamp"/> seconds since the Unix
|
||||
/// epoch.</returns>
|
||||
/// <param name="isMillis">
|
||||
/// Optional. Whether or not the input value should be treated as milliseconds. Defaults
|
||||
/// to <see langword="false" />..
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// Returns a <see cref="DateTime" /> representing <paramref name="timestamp" /> seconds since the Unix
|
||||
/// epoch.
|
||||
/// </returns>
|
||||
public static DateTime FromUnixTimestamp(this short timestamp, bool isMillis = false)
|
||||
{
|
||||
return ((long)timestamp).FromUnixTimestamp(isMillis);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="ushort"/> to a <see cref="byte"/>[].
|
||||
/// Converts the <see cref="ushort" /> to a <see cref="byte" />[].
|
||||
/// </summary>
|
||||
/// <param name="number">The number to convert.</param>
|
||||
/// <returns>Returns a <see cref="byte"/>[].</returns>
|
||||
/// <returns>Returns a <see cref="byte" />[].</returns>
|
||||
[CLSCompliant(false)]
|
||||
public static byte[] GetBytes(this ushort number)
|
||||
{
|
||||
@ -61,32 +69,36 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="short"/> to a <see cref="byte"/>[].
|
||||
/// Converts the <see cref="short" /> to a <see cref="byte" />[].
|
||||
/// </summary>
|
||||
/// <param name="number">The number to convert.</param>
|
||||
/// <returns>Returns a <see cref="byte"/>[].</returns>
|
||||
/// <returns>Returns a <see cref="byte" />[].</returns>
|
||||
public static byte[] GetBytes(this short number)
|
||||
{
|
||||
return BitConverter.GetBytes(number);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the <see cref="short"/> is even.
|
||||
/// Determines if the <see cref="short" /> is even.
|
||||
/// </summary>
|
||||
/// <param name="number">The number.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is even, <see langword="false"/>
|
||||
/// otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if <paramref name="number" /> is even, <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
public static bool IsEven(this short number)
|
||||
{
|
||||
return ((long)number).IsEven();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the <see cref="ushort"/> is even.
|
||||
/// Determines if the <see cref="ushort" /> is even.
|
||||
/// </summary>
|
||||
/// <param name="number">The number.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is even, <see langword="false"/>
|
||||
/// otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if <paramref name="number" /> is even, <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
[CLSCompliant(false)]
|
||||
public static bool IsEven(this ushort number)
|
||||
{
|
||||
@ -94,22 +106,26 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the <see cref="short"/> is odd.
|
||||
/// Determines if the <see cref="short" /> is odd.
|
||||
/// </summary>
|
||||
/// <param name="number">The number.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is odd, <see langword="false"/>
|
||||
/// otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if <paramref name="number" /> is odd, <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
public static bool IsOdd(this short number)
|
||||
{
|
||||
return !number.IsEven();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the <see cref="ushort"/> is odd.
|
||||
/// Determines if the <see cref="ushort" /> is odd.
|
||||
/// </summary>
|
||||
/// <param name="number">The number.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is odd, <see langword="false"/>
|
||||
/// otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if <paramref name="number" /> is odd, <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
[CLSCompliant(false)]
|
||||
public static bool IsOdd(this ushort number)
|
||||
{
|
||||
@ -117,22 +133,26 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the <see cref="short"/> is a prime number.
|
||||
/// Determines if the <see cref="short" /> is a prime number.
|
||||
/// </summary>
|
||||
/// <param name="number">The number.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is prime, <see langword="false"/>
|
||||
/// otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if <paramref name="number" /> is prime, <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
public static bool IsPrime(this short number)
|
||||
{
|
||||
return ((long)number).IsPrime();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the <see cref="short"/> is a prime number.
|
||||
/// Determines if the <see cref="short" /> is a prime number.
|
||||
/// </summary>
|
||||
/// <param name="number">The number.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is prime, <see langword="false"/>
|
||||
/// otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if <paramref name="number" /> is prime, <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
[CLSCompliant(false)]
|
||||
public static bool IsPrime(this ushort number)
|
||||
{
|
||||
@ -140,22 +160,26 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets an boolean value that represents this integer.
|
||||
/// Gets an boolean value that represents this integer.
|
||||
/// </summary>
|
||||
/// <param name="value">The integer.</param>
|
||||
/// <returns>Returns <see langword="false"/> if <paramref name="value"/> is 0,
|
||||
/// <see langword="true"/> otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="false" /> if <paramref name="value" /> is 0,
|
||||
/// <see langword="true" /> otherwise.
|
||||
/// </returns>
|
||||
public static bool ToBoolean(this short value)
|
||||
{
|
||||
return ((long)value).ToBoolean();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets an boolean value that represents this integer.
|
||||
/// Gets an boolean value that represents this integer.
|
||||
/// </summary>
|
||||
/// <param name="value">The integer.</param>
|
||||
/// <returns>Returns <see langword="false"/> if <paramref name="value"/> is 0,
|
||||
/// <see langword="true"/> otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="false" /> if <paramref name="value" /> is 0,
|
||||
/// <see langword="true" /> otherwise.
|
||||
/// </returns>
|
||||
[CLSCompliant(false)]
|
||||
public static bool ToBoolean(this ushort value)
|
||||
{
|
||||
|
@ -3,33 +3,37 @@
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="int"/>.
|
||||
/// Extension methods for <see cref="int" />.
|
||||
/// </summary>
|
||||
public static class Int32Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Clamps a value between a minimum and a maximum value.
|
||||
/// Clamps a value between a minimum and a maximum value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to clamp.</param>
|
||||
/// <param name="min">The minimum value.</param>
|
||||
/// <param name="max">The maximum value.</param>
|
||||
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
|
||||
/// <paramref name="min"/> if <paramref name="value"/> is less than it,
|
||||
/// or <paramref name="value"/> itself otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <paramref name="max" /> if <paramref name="value" /> is greater than it,
|
||||
/// <paramref name="min" /> if <paramref name="value" /> is less than it,
|
||||
/// or <paramref name="value" /> itself otherwise.
|
||||
/// </returns>
|
||||
public static int Clamp(this int value, int min, int max)
|
||||
{
|
||||
return Math.Min(Math.Max(value, min), max);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clamps a value between a minimum and a maximum value.
|
||||
/// Clamps a value between a minimum and a maximum value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to clamp.</param>
|
||||
/// <param name="min">The minimum value.</param>
|
||||
/// <param name="max">The maximum value.</param>
|
||||
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
|
||||
/// <paramref name="min"/> if <paramref name="value"/> is less than it,
|
||||
/// or <paramref name="value"/> itself otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <paramref name="max" /> if <paramref name="value" /> is greater than it,
|
||||
/// <paramref name="min" /> if <paramref name="value" /> is less than it,
|
||||
/// or <paramref name="value" /> itself otherwise.
|
||||
/// </returns>
|
||||
[CLSCompliant(false)]
|
||||
public static uint Clamp(this uint value, uint min, uint max)
|
||||
{
|
||||
@ -37,23 +41,27 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="int"/> to a <see cref="DateTime"/> treating it as a Unix timestamp.
|
||||
/// Converts the <see cref="int" /> to a <see cref="DateTime" /> treating it as a Unix timestamp.
|
||||
/// </summary>
|
||||
/// <param name="timestamp">The timestamp.</param>
|
||||
/// <param name="isMillis">Optional. Whether or not the input value should be treated as milliseconds. Defaults
|
||||
/// to <see langword="false"/>..</param>
|
||||
/// <returns>Returns a <see cref="DateTime"/> representing <paramref name="timestamp"/> seconds since the Unix
|
||||
/// epoch.</returns>
|
||||
/// <param name="isMillis">
|
||||
/// Optional. Whether or not the input value should be treated as milliseconds. Defaults
|
||||
/// to <see langword="false" />..
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// Returns a <see cref="DateTime" /> representing <paramref name="timestamp" /> seconds since the Unix
|
||||
/// epoch.
|
||||
/// </returns>
|
||||
public static DateTime FromUnixTimestamp(this int timestamp, bool isMillis = false)
|
||||
{
|
||||
return ((long)timestamp).FromUnixTimestamp(isMillis);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="uint"/> to a <see cref="byte"/>[].
|
||||
/// Converts the <see cref="uint" /> to a <see cref="byte" />[].
|
||||
/// </summary>
|
||||
/// <param name="number">The number to convert.</param>
|
||||
/// <returns>Returns a <see cref="byte"/>[].</returns>
|
||||
/// <returns>Returns a <see cref="byte" />[].</returns>
|
||||
[CLSCompliant(false)]
|
||||
public static byte[] GetBytes(this uint number)
|
||||
{
|
||||
@ -61,32 +69,36 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="int"/> to a <see cref="byte"/>[].
|
||||
/// Converts the <see cref="int" /> to a <see cref="byte" />[].
|
||||
/// </summary>
|
||||
/// <param name="number">The number to convert.</param>
|
||||
/// <returns>Returns a <see cref="byte"/>[].</returns>
|
||||
/// <returns>Returns a <see cref="byte" />[].</returns>
|
||||
public static byte[] GetBytes(this int number)
|
||||
{
|
||||
return BitConverter.GetBytes(number);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the <see cref="int"/> is even.
|
||||
/// Determines if the <see cref="int" /> is even.
|
||||
/// </summary>
|
||||
/// <param name="number">The number.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is even, <see langword="false"/>
|
||||
/// otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if <paramref name="number" /> is even, <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
public static bool IsEven(this int number)
|
||||
{
|
||||
return ((long)number).IsEven();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the <see cref="uint"/> is even.
|
||||
/// Determines if the <see cref="uint" /> is even.
|
||||
/// </summary>
|
||||
/// <param name="number">The number.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is even, <see langword="false"/>
|
||||
/// otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if <paramref name="number" /> is even, <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
[CLSCompliant(false)]
|
||||
public static bool IsEven(this uint number)
|
||||
{
|
||||
@ -94,22 +106,26 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the <see cref="int"/> is odd.
|
||||
/// Determines if the <see cref="int" /> is odd.
|
||||
/// </summary>
|
||||
/// <param name="number">The number.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is odd, <see langword="false"/>
|
||||
/// otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if <paramref name="number" /> is odd, <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
public static bool IsOdd(this int number)
|
||||
{
|
||||
return !number.IsEven();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the <see cref="uint"/> is odd.
|
||||
/// Determines if the <see cref="uint" /> is odd.
|
||||
/// </summary>
|
||||
/// <param name="number">The number.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is odd, <see langword="false"/>
|
||||
/// otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if <paramref name="number" /> is odd, <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
[CLSCompliant(false)]
|
||||
public static bool IsOdd(this uint number)
|
||||
{
|
||||
@ -117,33 +133,39 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the <see cref="int"/> is a prime number.
|
||||
/// Determines if the <see cref="int" /> is a prime number.
|
||||
/// </summary>
|
||||
/// <param name="number">The number.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is prime, <see langword="false"/>
|
||||
/// otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if <paramref name="number" /> is prime, <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
public static bool IsPrime(this int number)
|
||||
{
|
||||
return ((long)number).IsPrime();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets an boolean value that represents this integer.
|
||||
/// Gets an boolean value that represents this integer.
|
||||
/// </summary>
|
||||
/// <param name="value">The integer.</param>
|
||||
/// <returns>Returns <see langword="false"/> if <paramref name="value"/> is 0,
|
||||
/// <see langword="true"/> otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="false" /> if <paramref name="value" /> is 0,
|
||||
/// <see langword="true" /> otherwise.
|
||||
/// </returns>
|
||||
public static bool ToBoolean(this int value)
|
||||
{
|
||||
return ((long)value).ToBoolean();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets an boolean value that represents this integer.
|
||||
/// Gets an boolean value that represents this integer.
|
||||
/// </summary>
|
||||
/// <param name="value">The integer.</param>
|
||||
/// <returns>Returns <see langword="false"/> if <paramref name="value"/> is 0,
|
||||
/// <see langword="true"/> otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="false" /> if <paramref name="value" /> is 0,
|
||||
/// <see langword="true" /> otherwise.
|
||||
/// </returns>
|
||||
[CLSCompliant(false)]
|
||||
public static bool ToBoolean(this uint value)
|
||||
{
|
||||
|
@ -3,33 +3,37 @@
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="long"/>.
|
||||
/// Extension methods for <see cref="long" />.
|
||||
/// </summary>
|
||||
public static class Int64Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Clamps a value between a minimum and a maximum value.
|
||||
/// Clamps a value between a minimum and a maximum value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to clamp.</param>
|
||||
/// <param name="min">The minimum value.</param>
|
||||
/// <param name="max">The maximum value.</param>
|
||||
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
|
||||
/// <paramref name="min"/> if <paramref name="value"/> is less than it,
|
||||
/// or <paramref name="value"/> itself otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <paramref name="max" /> if <paramref name="value" /> is greater than it,
|
||||
/// <paramref name="min" /> if <paramref name="value" /> is less than it,
|
||||
/// or <paramref name="value" /> itself otherwise.
|
||||
/// </returns>
|
||||
public static long Clamp(this long value, long min, long max)
|
||||
{
|
||||
return Math.Min(Math.Max(value, min), max);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clamps a value between a minimum and a maximum value.
|
||||
/// Clamps a value between a minimum and a maximum value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to clamp.</param>
|
||||
/// <param name="min">The minimum value.</param>
|
||||
/// <param name="max">The maximum value.</param>
|
||||
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
|
||||
/// <paramref name="min"/> if <paramref name="value"/> is less than it,
|
||||
/// or <paramref name="value"/> itself otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <paramref name="max" /> if <paramref name="value" /> is greater than it,
|
||||
/// <paramref name="min" /> if <paramref name="value" /> is less than it,
|
||||
/// or <paramref name="value" /> itself otherwise.
|
||||
/// </returns>
|
||||
[CLSCompliant(false)]
|
||||
public static ulong Clamp(this ulong value, ulong min, ulong max)
|
||||
{
|
||||
@ -37,16 +41,20 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="long"/> to a <see cref="DateTime"/> treating it as a Unix timestamp.
|
||||
/// Converts the <see cref="long" /> to a <see cref="DateTime" /> treating it as a Unix timestamp.
|
||||
/// </summary>
|
||||
/// <param name="timestamp">The timestamp.</param>
|
||||
/// <param name="isMillis">Optional. Whether or not the input value should be treated as milliseconds. Defaults
|
||||
/// to <see langword="false"/>.</param>
|
||||
/// <returns>Returns a <see cref="DateTime"/> representing <paramref name="timestamp"/> seconds since the Unix
|
||||
/// epoch.</returns>
|
||||
/// <param name="isMillis">
|
||||
/// Optional. Whether or not the input value should be treated as milliseconds. Defaults
|
||||
/// to <see langword="false" />.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// Returns a <see cref="DateTime" /> representing <paramref name="timestamp" /> seconds since the Unix
|
||||
/// epoch.
|
||||
/// </returns>
|
||||
public static DateTime FromUnixTimestamp(this long timestamp, bool isMillis = false)
|
||||
{
|
||||
DateTimeOffset offset = isMillis
|
||||
var offset = isMillis
|
||||
? DateTimeOffset.FromUnixTimeMilliseconds(timestamp)
|
||||
: DateTimeOffset.FromUnixTimeSeconds(timestamp);
|
||||
|
||||
@ -54,10 +62,10 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="ulong"/> to a <see cref="byte"/>[].
|
||||
/// Converts the <see cref="ulong" /> to a <see cref="byte" />[].
|
||||
/// </summary>
|
||||
/// <param name="number">The number to convert.</param>
|
||||
/// <returns>Returns a <see cref="byte"/>[].</returns>
|
||||
/// <returns>Returns a <see cref="byte" />[].</returns>
|
||||
[CLSCompliant(false)]
|
||||
public static byte[] GetBytes(this ulong number)
|
||||
{
|
||||
@ -65,32 +73,36 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="long"/> to a <see cref="byte"/>[].
|
||||
/// Converts the <see cref="long" /> to a <see cref="byte" />[].
|
||||
/// </summary>
|
||||
/// <param name="number">The number to convert.</param>
|
||||
/// <returns>Returns a <see cref="byte"/>[].</returns>
|
||||
/// <returns>Returns a <see cref="byte" />[].</returns>
|
||||
public static byte[] GetBytes(this long number)
|
||||
{
|
||||
return BitConverter.GetBytes(number);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the <see cref="long"/> is even.
|
||||
/// Determines if the <see cref="long" /> is even.
|
||||
/// </summary>
|
||||
/// <param name="number">The number.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is even, <see langword="false"/>
|
||||
/// otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if <paramref name="number" /> is even, <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
public static bool IsEven(this long number)
|
||||
{
|
||||
return Math.Abs(number % 2.0) < double.Epsilon;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the <see cref="ulong"/> is even.
|
||||
/// Determines if the <see cref="ulong" /> is even.
|
||||
/// </summary>
|
||||
/// <param name="number">The number.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is even, <see langword="false"/>
|
||||
/// otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if <paramref name="number" /> is even, <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
[CLSCompliant(false)]
|
||||
public static bool IsEven(this ulong number)
|
||||
{
|
||||
@ -98,22 +110,26 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the <see cref="long"/> is odd.
|
||||
/// Determines if the <see cref="long" /> is odd.
|
||||
/// </summary>
|
||||
/// <param name="number">The number.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is odd, <see langword="false"/>
|
||||
/// otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if <paramref name="number" /> is odd, <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
public static bool IsOdd(this long number)
|
||||
{
|
||||
return !IsEven(number);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the <see cref="ulong"/> is odd.
|
||||
/// Determines if the <see cref="ulong" /> is odd.
|
||||
/// </summary>
|
||||
/// <param name="number">The number.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is odd, <see langword="false"/>
|
||||
/// otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if <paramref name="number" /> is odd, <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
[CLSCompliant(false)]
|
||||
public static bool IsOdd(this ulong number)
|
||||
{
|
||||
@ -121,11 +137,13 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the <see cref="long"/> is a prime number.
|
||||
/// Determines if the <see cref="long" /> is a prime number.
|
||||
/// </summary>
|
||||
/// <param name="number">The number.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is prime, <see langword="false"/>
|
||||
/// otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if <paramref name="number" /> is prime, <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
public static bool IsPrime(this long number)
|
||||
{
|
||||
if (number <= 1)
|
||||
@ -156,11 +174,13 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the <see cref="ulong"/> is a prime number.
|
||||
/// Determines if the <see cref="ulong" /> is a prime number.
|
||||
/// </summary>
|
||||
/// <param name="number">The number.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is prime, <see langword="false"/>
|
||||
/// otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if <paramref name="number" /> is prime, <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
[CLSCompliant(false)]
|
||||
public static bool IsPrime(this ulong number)
|
||||
{
|
||||
@ -192,22 +212,26 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets an boolean value that represents this integer.
|
||||
/// Gets an boolean value that represents this integer.
|
||||
/// </summary>
|
||||
/// <param name="value">The integer.</param>
|
||||
/// <returns>Returns <see langword="false"/> if <paramref name="value"/> is 0,
|
||||
/// <see langword="true"/> otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="false" /> if <paramref name="value" /> is 0,
|
||||
/// <see langword="true" /> otherwise.
|
||||
/// </returns>
|
||||
public static bool ToBoolean(this long value)
|
||||
{
|
||||
return value != 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets an boolean value that represents this integer.
|
||||
/// Gets an boolean value that represents this integer.
|
||||
/// </summary>
|
||||
/// <param name="value">The integer.</param>
|
||||
/// <returns>Returns <see langword="false"/> if <paramref name="value"/> is 0,
|
||||
/// <see langword="true"/> otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="false" /> if <paramref name="value" /> is 0,
|
||||
/// <see langword="true" /> otherwise.
|
||||
/// </returns>
|
||||
[CLSCompliant(false)]
|
||||
public static bool ToBoolean(this ulong value)
|
||||
{
|
||||
|
@ -5,97 +5,97 @@
|
||||
using System.Linq;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="IList{T}"/>.
|
||||
/// Extension methods for <see cref="IList{T}" />.
|
||||
/// </summary>
|
||||
public static class ListExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns a random element from <paramref name="source"/> using a new <see cref="Random"/> instance.
|
||||
/// Returns a random element from <paramref name="source" /> using a new <see cref="Random" /> instance.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The collection type.</typeparam>
|
||||
/// <param name="source">The collection to draw from.</param>
|
||||
/// <returns>Returns a random element of type <see cref="T"/> from <paramref name="source"/>.</returns>
|
||||
/// <returns>Returns a random element of type <see cref="T" /> from <paramref name="source" />.</returns>
|
||||
public static T OneOf<T>(this IEnumerable<T> source)
|
||||
{
|
||||
return source.OneOf(new Random());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a random element from <paramref name="source"/> using the <see cref="Random"/> instance.
|
||||
/// Returns a random element from <paramref name="source" /> using the <see cref="Random" /> instance.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The collection type.</typeparam>
|
||||
/// <param name="source">The collection to draw from.</param>
|
||||
/// <param name="random">The <see cref="Random"/> instance.</param>
|
||||
/// <returns>Returns a random element of type <see cref="T"/> from <paramref name="source"/>.</returns>
|
||||
/// <param name="random">The <see cref="Random" /> instance.</param>
|
||||
/// <returns>Returns a random element of type <see cref="T" /> from <paramref name="source" />.</returns>
|
||||
public static T OneOf<T>(this IEnumerable<T> source, Random random)
|
||||
{
|
||||
return source.ToList().OneOf(random);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a random element from <paramref name="source"/> using a new <see cref="Random"/> instance.
|
||||
/// Returns a random element from <paramref name="source" /> using a new <see cref="Random" /> instance.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The collection type.</typeparam>
|
||||
/// <param name="source">The collection to draw from.</param>
|
||||
/// <returns>Returns a random element of type <see cref="T"/> from <paramref name="source"/>.</returns>
|
||||
/// <returns>Returns a random element of type <see cref="T" /> from <paramref name="source" />.</returns>
|
||||
public static T OneOf<T>(this IList<T> source)
|
||||
{
|
||||
return source.OneOf(new Random());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a random element from <paramref name="source"/> using the <see cref="Random"/> instance.
|
||||
/// Returns a random element from <paramref name="source" /> using the <see cref="Random" /> instance.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The collection type.</typeparam>
|
||||
/// <param name="source">The collection to draw from.</param>
|
||||
/// <param name="random">The <see cref="Random"/> instance.</param>
|
||||
/// <returns>Returns a random element of type <see cref="T"/> from <paramref name="source"/>.</returns>
|
||||
/// <param name="random">The <see cref="Random" /> instance.</param>
|
||||
/// <returns>Returns a random element of type <see cref="T" /> from <paramref name="source" />.</returns>
|
||||
public static T OneOf<T>(this IList<T> source, Random random)
|
||||
{
|
||||
return random.OneOf(source);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shuffles an enumerable.
|
||||
/// Shuffles an enumerable.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The collection type.</typeparam>
|
||||
/// <param name="source">The collection to shuffle.</param>
|
||||
/// <returns>Returns <paramref name="source"/> shuffled.</returns>
|
||||
/// <returns>Returns <paramref name="source" /> shuffled.</returns>
|
||||
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)
|
||||
{
|
||||
return source.Shuffle(new Random());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shuffles an enumerable.
|
||||
/// Shuffles an enumerable.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The collection type.</typeparam>
|
||||
/// <param name="source">The collection to shuffle.</param>
|
||||
/// <param name="random">The <see cref="Random"/> instance.</param>
|
||||
/// <returns>Returns <paramref name="source"/> shuffled.</returns>
|
||||
/// <param name="random">The <see cref="Random" /> instance.</param>
|
||||
/// <returns>Returns <paramref name="source" /> shuffled.</returns>
|
||||
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random random)
|
||||
{
|
||||
return source.OrderBy(_ => random.Next());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shuffles a list.
|
||||
/// Shuffles a list.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The collection type.</typeparam>
|
||||
/// <param name="source">The collection to shuffle.</param>
|
||||
/// <returns>Returns <paramref name="source"/> shuffled.</returns>
|
||||
/// <returns>Returns <paramref name="source" /> shuffled.</returns>
|
||||
public static IEnumerable<T> Shuffle<T>(this IList<T> source)
|
||||
{
|
||||
return source.Shuffle(new Random());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shuffles a list.
|
||||
/// Shuffles a list.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The collection type.</typeparam>
|
||||
/// <param name="source">The collection to shuffle.</param>
|
||||
/// <param name="random">The <see cref="Random"/> instance.</param>
|
||||
/// <returns>Returns <paramref name="source"/> shuffled.</returns>
|
||||
/// <param name="random">The <see cref="Random" /> instance.</param>
|
||||
/// <returns>Returns <paramref name="source" /> shuffled.</returns>
|
||||
public static IEnumerable<T> Shuffle<T>(this IList<T> source, Random random)
|
||||
{
|
||||
return source.OrderBy(_ => random.Next());
|
||||
|
@ -5,24 +5,26 @@
|
||||
using System.Linq;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="Random"/>.
|
||||
/// Extension methods for <see cref="Random" />.
|
||||
/// </summary>
|
||||
public static class RandomExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the <see cref="System.Random"/> instance to which other extension methods may refer, when one is
|
||||
/// needed but not provided.
|
||||
/// Gets the <see cref="System.Random" /> instance to which other extension methods may refer, when one is
|
||||
/// needed but not provided.
|
||||
/// </summary>
|
||||
internal static Random Random { get; } = new Random();
|
||||
|
||||
/// <summary>
|
||||
/// Returns either <see langword="true"/> or <see langword="false"/> based on <paramref name="random"/>'s next
|
||||
/// generation.
|
||||
/// Returns either <see langword="true" /> or <see langword="false" /> based on <paramref name="random" />'s next
|
||||
/// generation.
|
||||
/// </summary>
|
||||
/// <param name="random">The <see cref="System.Random"/> instance.</param>
|
||||
/// <returns>Returns <see langword="true"/> or <see langword="false"/> depending on the return value
|
||||
/// from <see cref="System.Random.Next(int)"/>.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="random"/> is <see langword="null"/>.</exception>
|
||||
/// <param name="random">The <see cref="System.Random" /> instance.</param>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> or <see langword="false" /> depending on the return value
|
||||
/// from <see cref="System.Random.Next(int)" />.
|
||||
/// </returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
|
||||
public static bool CoinToss(this Random random)
|
||||
{
|
||||
if (random is null)
|
||||
@ -34,26 +36,28 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a random element from <paramref name="source"/> using the <see cref="System.Random"/> instance.
|
||||
/// Returns a random element from <paramref name="source" /> using the <see cref="System.Random" /> instance.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The collection type.</typeparam>
|
||||
/// <param name="random">The <see cref="System.Random"/> instance.</param>
|
||||
/// <param name="random">The <see cref="System.Random" /> instance.</param>
|
||||
/// <param name="source">The collection from which to draw.</param>
|
||||
/// <returns>Returns a random element of type <see cref="T"/> from <paramref name="source"/>.</returns>
|
||||
/// <returns>Returns a random element of type <see cref="T" /> from <paramref name="source" />.</returns>
|
||||
public static T OneOf<T>(this Random random, params T[] source)
|
||||
{
|
||||
return source.ToList().OneOf(random);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a random element from <paramref name="source"/> using the <see cref="System.Random"/> instance.
|
||||
/// Returns a random element from <paramref name="source" /> using the <see cref="System.Random" /> instance.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The collection type.</typeparam>
|
||||
/// <param name="random">The <see cref="System.Random"/> instance.</param>
|
||||
/// <param name="random">The <see cref="System.Random" /> instance.</param>
|
||||
/// <param name="source">The collection from which to draw.</param>
|
||||
/// <returns>Returns a random element of type <see cref="T"/> from <paramref name="source"/>.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="random"/> or <paramref name="source"/> is
|
||||
/// <see langword="null"/>.</exception>
|
||||
/// <returns>Returns a random element of type <see cref="T" /> from <paramref name="source" />.</returns>
|
||||
/// <exception cref="ArgumentNullException">
|
||||
/// <paramref name="random" /> or <paramref name="source" /> is
|
||||
/// <see langword="null" />.
|
||||
/// </exception>
|
||||
public static T OneOf<T>(this Random random, IList<T> source)
|
||||
{
|
||||
if (random is null)
|
||||
|
@ -5,18 +5,20 @@
|
||||
using System.Reflection;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for various reflection types.
|
||||
/// Extension methods for various reflection types.
|
||||
/// </summary>
|
||||
public static class ReflectionExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the value set in this member's annotated <see cref="DefaultValueAttribute"/>, or
|
||||
/// <see langword="default"/> if none exists.
|
||||
/// Gets the value set in this member's annotated <see cref="DefaultValueAttribute" />, or
|
||||
/// <see langword="default" /> if none exists.
|
||||
/// </summary>
|
||||
/// <param name="member">The member.</param>
|
||||
/// <returns>Returns an <see cref="object"/> representing the value stored in this member's
|
||||
/// <see cref="DefaultValueAttribute"/>.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="member"/> is <see langword="null"/>.</exception>
|
||||
/// <returns>
|
||||
/// Returns an <see cref="object" /> representing the value stored in this member's
|
||||
/// <see cref="DefaultValueAttribute" />.
|
||||
/// </returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="member" /> is <see langword="null" />.</exception>
|
||||
public static object GetDefaultValue(this MemberInfo member)
|
||||
{
|
||||
if (member is null)
|
||||
@ -33,14 +35,16 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the value set in this member's annotated <see cref="DefaultValueAttribute"/>, or
|
||||
/// <see langword="default"/> if none exists.
|
||||
/// Gets the value set in this member's annotated <see cref="DefaultValueAttribute" />, or
|
||||
/// <see langword="default" /> if none exists.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to which the value should cast.</typeparam>
|
||||
/// <param name="member">The member.</param>
|
||||
/// <returns>Returns an instance of <typeparamref name="T"/> representing the value stored in this member's
|
||||
/// <see cref="DefaultValueAttribute"/>.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="member"/> is <see langword="null"/>.</exception>
|
||||
/// <returns>
|
||||
/// Returns an instance of <typeparamref name="T" /> representing the value stored in this member's
|
||||
/// <see cref="DefaultValueAttribute" />.
|
||||
/// </returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="member" /> is <see langword="null" />.</exception>
|
||||
public static T GetDefaultValue<T>(this MemberInfo member)
|
||||
{
|
||||
if (member is null)
|
||||
@ -52,13 +56,15 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the value set in this member's annotated <see cref="DescriptionAttribute"/>, or
|
||||
/// <see langword="null"/> if none exists.
|
||||
/// Gets the value set in this member's annotated <see cref="DescriptionAttribute" />, or
|
||||
/// <see langword="null" /> if none exists.
|
||||
/// </summary>
|
||||
/// <param name="member">The member.</param>
|
||||
/// <returns>Returns an instance of <typeparamref name="T"/> representing the value stored in this member's
|
||||
/// <see cref="DescriptionAttribute"/>.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="member"/> is <see langword="null"/>.</exception>
|
||||
/// <returns>
|
||||
/// Returns a string representing the value stored in this member's
|
||||
/// <see cref="DescriptionAttribute" />.
|
||||
/// </returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="member" /> is <see langword="null" />.</exception>
|
||||
public static string GetDescription(this MemberInfo member)
|
||||
{
|
||||
if (member is null)
|
||||
@ -75,18 +81,22 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves a custom attribute of a specified type that is applied to the specified member, and passes it
|
||||
/// to a selector delegate in order to select one or more the members in the attribute.
|
||||
/// Retrieves a custom attribute of a specified type that is applied to the specified member, and passes it
|
||||
/// to a selector delegate in order to select one or more the members in the attribute.
|
||||
/// </summary>
|
||||
/// <typeparam name="TAttribute">The attribute type.</typeparam>
|
||||
/// <typeparam name="TReturn">The return type of the <paramref name="selector"/> delegate.</typeparam>
|
||||
/// <typeparam name="TReturn">The return type of the <paramref name="selector" /> delegate.</typeparam>
|
||||
/// <param name="member">The member.</param>
|
||||
/// <param name="selector">The selector delegate.</param>
|
||||
/// <returns>Returns an instance of <typeparamref name="TReturn"/> as provided from
|
||||
/// <paramref name="selector"/>.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="member"/> is <see langword="null"/>
|
||||
/// -or-
|
||||
/// <paramref name="selector"/> is <see langword="null"/>.</exception>
|
||||
/// <returns>
|
||||
/// Returns an instance of <typeparamref name="TReturn" /> as provided from
|
||||
/// <paramref name="selector" />.
|
||||
/// </returns>
|
||||
/// <exception cref="ArgumentNullException">
|
||||
/// <paramref name="member" /> is <see langword="null" />
|
||||
/// -or-
|
||||
/// <paramref name="selector" /> is <see langword="null" />.
|
||||
/// </exception>
|
||||
public static TReturn SelectFromCustomAttribute<TAttribute, TReturn>(
|
||||
this MemberInfo member,
|
||||
Func<TAttribute, TReturn> selector)
|
||||
|
@ -3,83 +3,89 @@
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="float"/>.
|
||||
/// Extension methods for <see cref="float" />.
|
||||
/// </summary>
|
||||
public static class SingleExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Clamps a value between a minimum and a maximum value.
|
||||
/// Clamps a value between a minimum and a maximum value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to clamp.</param>
|
||||
/// <param name="min">The minimum value.</param>
|
||||
/// <param name="max">The maximum value.</param>
|
||||
/// <returns>Returns <paramref name="max"/> if <paramref name="value"/> is greater than it,
|
||||
/// <paramref name="min"/> if <paramref name="value"/> is less than it,
|
||||
/// or <paramref name="value"/> itself otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <paramref name="max" /> if <paramref name="value" /> is greater than it,
|
||||
/// <paramref name="min" /> if <paramref name="value" /> is less than it,
|
||||
/// or <paramref name="value" /> itself otherwise.
|
||||
/// </returns>
|
||||
public static float Clamp(this float value, float min, float max)
|
||||
{
|
||||
return Math.Min(Math.Max(value, min), max);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts an angle from degrees to radians.
|
||||
/// Converts an angle from degrees to radians.
|
||||
/// </summary>
|
||||
/// <param name="angle">The angle in degrees.</param>
|
||||
/// <returns>Returns <paramref name="angle"/> in radians.</returns>
|
||||
/// <returns>Returns <paramref name="angle" /> in radians.</returns>
|
||||
public static float DegreesToRadians(this float angle)
|
||||
{
|
||||
return (float)((double)angle).DegreesToRadians();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the <see cref="float"/> to a <see cref="byte"/>[].
|
||||
/// Converts the <see cref="float" /> to a <see cref="byte" />[].
|
||||
/// </summary>
|
||||
/// <param name="number">The number to convert.</param>
|
||||
/// <returns>Returns a <see cref="byte"/>[].</returns>
|
||||
/// <returns>Returns a <see cref="byte" />[].</returns>
|
||||
public static byte[] GetBytes(this float number)
|
||||
{
|
||||
return BitConverter.GetBytes(number);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the <see cref="float"/> is even.
|
||||
/// Determines if the <see cref="float" /> is even.
|
||||
/// </summary>
|
||||
/// <param name="number">The number.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is even, <see langword="false"/>
|
||||
/// otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if <paramref name="number" /> is even, <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
public static bool IsEven(this float number)
|
||||
{
|
||||
return ((double)number).IsEven();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the <see cref="float"/> is odd.
|
||||
/// Determines if the <see cref="float" /> is odd.
|
||||
/// </summary>
|
||||
/// <param name="number">The number.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="number"/> is odd, <see langword="false"/>
|
||||
/// otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if <paramref name="number" /> is odd, <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
public static bool IsOdd(this float number)
|
||||
{
|
||||
return !number.IsEven();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts an angle from radians to degrees.
|
||||
/// Converts an angle from radians to degrees.
|
||||
/// </summary>
|
||||
/// <param name="angle">The angle in radians.</param>
|
||||
/// <returns>Returns <paramref name="angle"/> in degrees.</returns>
|
||||
/// <returns>Returns <paramref name="angle" /> in degrees.</returns>
|
||||
public static float RadiansToDegrees(this float angle)
|
||||
{
|
||||
return (float)((double)angle).RadiansToDegrees();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rounds to the nearest value.
|
||||
/// Rounds to the nearest value.
|
||||
/// </summary>
|
||||
/// <param name="v">The value to round.</param>
|
||||
/// <param name="nearest">The nearest value.</param>
|
||||
/// <returns>Returns the rounded value.</returns>
|
||||
public static float Round(this float v, int nearest = 1)
|
||||
public static float Round(this float v, float nearest = 1)
|
||||
{
|
||||
return (float)((double)v).Round(nearest);
|
||||
}
|
||||
|
@ -2,21 +2,20 @@
|
||||
{
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="Stream"/>.
|
||||
/// Extension methods for <see cref="Stream" />.
|
||||
/// </summary>
|
||||
public static class StreamExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns the hash of a stream using the specified hashing algorithm.
|
||||
/// Returns the hash of a stream using the specified hashing algorithm.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">A <see cref="HashAlgorithm"/> derived type.</typeparam>
|
||||
/// <typeparam name="T">A <see cref="HashAlgorithm" /> derived type.</typeparam>
|
||||
/// <param name="stream">The stream whose hash is to be computed.</param>
|
||||
/// <returns>Returns a <see cref="byte"/> array representing the hash of the stream.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="stream"/> is <see langword="null"/>.</exception>
|
||||
/// <returns>Returns a <see cref="byte" /> array representing the hash of the stream.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="stream" /> is <see langword="null" />.</exception>
|
||||
public static byte[] GetHash<T>(this Stream stream)
|
||||
where T : HashAlgorithm
|
||||
{
|
||||
@ -25,7 +24,7 @@
|
||||
throw new ArgumentNullException(nameof(stream));
|
||||
}
|
||||
|
||||
MethodInfo create = typeof(T).GetMethod("Create", Array.Empty<Type>());
|
||||
var create = typeof(T).GetMethod("Create", Array.Empty<Type>());
|
||||
using var crypt = (T)create?.Invoke(null, null);
|
||||
return crypt?.ComputeHash(stream);
|
||||
}
|
||||
|
@ -8,12 +8,12 @@
|
||||
using System.Text;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="string"/>.
|
||||
/// Extension methods for <see cref="string" />.
|
||||
/// </summary>
|
||||
public static class StringExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Decodes a base-64 encoded string.
|
||||
/// Decodes a base-64 encoded string.
|
||||
/// </summary>
|
||||
/// <param name="data">The base-64 string to decode.</param>
|
||||
/// <returns>Returns the string in plain text.</returns>
|
||||
@ -23,7 +23,7 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encodes a base-64 encoded string.
|
||||
/// Encodes a base-64 encoded string.
|
||||
/// </summary>
|
||||
/// <param name="value">The plain text string to decode.</param>
|
||||
/// <returns>Returns the string in plain text.</returns>
|
||||
@ -33,18 +33,22 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts this string from one encoding to another.
|
||||
/// Converts this string from one encoding to another.
|
||||
/// </summary>
|
||||
/// <param name="str">The input string.</param>
|
||||
/// <param name="from">The input encoding.</param>
|
||||
/// <param name="to">The output encoding.</param>
|
||||
/// <returns>Returns a new <see cref="string"/> with its data converted to
|
||||
/// <paramref name="to"/>.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="str"/> is <see langword="null"/>
|
||||
/// - or -
|
||||
/// <paramref name="from"/> is <see langword="null"/>
|
||||
/// -or
|
||||
/// <paramref name="to"/> is <see langword="null"/>.</exception>
|
||||
/// <returns>
|
||||
/// Returns a new <see cref="string" /> with its data converted to
|
||||
/// <paramref name="to" />.
|
||||
/// </returns>
|
||||
/// <exception cref="ArgumentNullException">
|
||||
/// <paramref name="str" /> is <see langword="null" />
|
||||
/// - or -
|
||||
/// <paramref name="from" /> is <see langword="null" />
|
||||
/// -or
|
||||
/// <paramref name="to" /> is <see langword="null" />.
|
||||
/// </exception>
|
||||
public static string ChangeEncoding(this string str, Encoding from, Encoding to)
|
||||
{
|
||||
if (str is null)
|
||||
@ -66,27 +70,31 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses a <see cref="string"/> into an <see cref="Enum"/>.
|
||||
/// Parses a <see cref="string" /> into an <see cref="Enum" />.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the <see cref="Enum"/>.</typeparam>
|
||||
/// <param name="value">The <see cref="string"/> value to parse.</param>
|
||||
/// <returns>The <see cref="Enum"/> value corresponding to the <see cref="string"/>.</returns>
|
||||
/// <remarks>Credit for this method goes to Scott Dorman:
|
||||
/// (http://geekswithblogs.net/sdorman/Default.aspx)</remarks>
|
||||
/// <typeparam name="T">The type of the <see cref="Enum" />.</typeparam>
|
||||
/// <param name="value">The <see cref="string" /> value to parse.</param>
|
||||
/// <returns>The <see cref="Enum" /> value corresponding to the <see cref="string" />.</returns>
|
||||
/// <remarks>
|
||||
/// Credit for this method goes to Scott Dorman:
|
||||
/// (http://geekswithblogs.net/sdorman/Default.aspx).
|
||||
/// </remarks>
|
||||
public static T EnumParse<T>(this string value)
|
||||
{
|
||||
return value.EnumParse<T>(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses a <see cref="string"/> into an <see cref="Enum"/>.
|
||||
/// Parses a <see cref="string" /> into an <see cref="Enum" />.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the <see cref="Enum"/>.</typeparam>
|
||||
/// <param name="value">The <see cref="string"/> value to parse.</param>
|
||||
/// <typeparam name="T">The type of the <see cref="Enum" />.</typeparam>
|
||||
/// <param name="value">The <see cref="string" /> value to parse.</param>
|
||||
/// <param name="ignoreCase">Whether or not to ignore casing.</param>
|
||||
/// <returns>The <see cref="Enum"/> value corresponding to the <see cref="string"/>.</returns>
|
||||
/// <remarks>Credit for this method goes to Scott Dorman:
|
||||
/// (http://geekswithblogs.net/sdorman/Default.aspx)</remarks>
|
||||
/// <returns>The <see cref="Enum" /> value corresponding to the <see cref="string" />.</returns>
|
||||
/// <remarks>
|
||||
/// Credit for this method goes to Scott Dorman:
|
||||
/// (http://geekswithblogs.net/sdorman/Default.aspx).
|
||||
/// </remarks>
|
||||
public static T EnumParse<T>(this string value, bool ignoreCase)
|
||||
{
|
||||
if (value is null)
|
||||
@ -101,7 +109,7 @@
|
||||
throw new ArgumentException(Resource.EnumParseEmptyStringException, nameof(value));
|
||||
}
|
||||
|
||||
Type t = typeof(T);
|
||||
var t = typeof(T);
|
||||
|
||||
if (!t.IsEnum)
|
||||
{
|
||||
@ -112,24 +120,26 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="byte"/>[] representing the value the <see cref="string"/> with
|
||||
/// <see cref="Encoding.UTF8"/> encoding.
|
||||
/// Gets a <see cref="byte" />[] representing the value the <see cref="string" /> with
|
||||
/// <see cref="Encoding.UTF8" /> encoding.
|
||||
/// </summary>
|
||||
/// <param name="str">The string to convert.</param>
|
||||
/// <returns>Returns a <see cref="byte"/>[].</returns>
|
||||
/// <returns>Returns a <see cref="byte" />[].</returns>
|
||||
public static byte[] GetBytes(this string str)
|
||||
{
|
||||
return str.GetBytes(Encoding.UTF8);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="byte"/>[] representing the value the <see cref="string"/> with the provided encoding.
|
||||
/// Gets a <see cref="byte" />[] representing the value the <see cref="string" /> with the provided encoding.
|
||||
/// </summary>
|
||||
/// <param name="str">The string to convert.</param>
|
||||
/// <param name="encoding">The encoding to use.</param>
|
||||
/// <returns>Returns a <see cref="byte"/>[].</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="str"/> or <paramref name="encoding"/> or both are
|
||||
/// <see langword="null"/>.</exception>
|
||||
/// <returns>Returns a <see cref="byte" />[].</returns>
|
||||
/// <exception cref="ArgumentNullException">
|
||||
/// <paramref name="str" /> or <paramref name="encoding" /> or both are
|
||||
/// <see langword="null" />.
|
||||
/// </exception>
|
||||
public static byte[] GetBytes(this string str, Encoding encoding)
|
||||
{
|
||||
if (str is null)
|
||||
@ -146,46 +156,50 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if all alpha characters in this string are considered lowercase.
|
||||
/// Determines if all alpha characters in this string are considered lowercase.
|
||||
/// </summary>
|
||||
/// <param name="str">The input string.</param>
|
||||
/// <returns>Returns <see langword="true"/> if all alpha characters are lowercase, <see langword="false"/>
|
||||
/// otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if all alpha characters are lowercase, <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
public static bool IsLower(this string str)
|
||||
{
|
||||
return str.Where(char.IsLetter).All(char.IsLower);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if all alpha characters in this string are considered uppercase.
|
||||
/// Determines if all alpha characters in this string are considered uppercase.
|
||||
/// </summary>
|
||||
/// <param name="str">The input string.</param>
|
||||
/// <returns>Returns <see langword="true"/> if all alpha characters are uppercase, <see langword="false"/>
|
||||
/// otherwise.</returns>
|
||||
/// <returns>
|
||||
/// Returns <see langword="true" /> if all alpha characters are uppercase, <see langword="false" />
|
||||
/// otherwise.
|
||||
/// </returns>
|
||||
public static bool IsUpper(this string str)
|
||||
{
|
||||
return str.Where(char.IsLetter).All(char.IsUpper);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a new random string by filling it with characters found in <see cref="str"/>.
|
||||
/// Generates a new random string by filling it with characters found in <see cref="str" />.
|
||||
/// </summary>
|
||||
/// <param name="str">The character set.</param>
|
||||
/// <param name="length">The length of the string to generate.</param>
|
||||
/// <returns>Returns a <see cref="string"/> containing <paramref name="length"/> characters.</returns>
|
||||
/// <returns>Returns a <see cref="string" /> containing <paramref name="length" /> characters.</returns>
|
||||
public static string Random(this string str, int length)
|
||||
{
|
||||
return str.Random(length, RandomExtensions.Random);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a new random string by filling it with characters found in <see cref="str"/>.
|
||||
/// Generates a new random string by filling it with characters found in <see cref="str" />.
|
||||
/// </summary>
|
||||
/// <param name="str">The character set.</param>
|
||||
/// <param name="length">The length of the string to generate.</param>
|
||||
/// <param name="random">The <see cref="System.Random"/> instance.</param>
|
||||
/// <returns>Returns a <see cref="string"/> containing <paramref name="length"/> characters.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="str"/> is <see langword="null"/>.</exception>
|
||||
/// <param name="random">The <see cref="System.Random" /> instance.</param>
|
||||
/// <returns>Returns a <see cref="string" /> containing <paramref name="length" /> characters.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="str" /> is <see langword="null" />.</exception>
|
||||
public static string Random(this string str, int length, Random random)
|
||||
{
|
||||
if (str is null)
|
||||
@ -197,13 +211,15 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Repeats a string a specified number of times.
|
||||
/// Repeats a string a specified number of times.
|
||||
/// </summary>
|
||||
/// <param name="str">The string to repeat.</param>
|
||||
/// <param name="count">The repeat count.</param>
|
||||
/// <returns>Returns a <see cref="string"/> whose value is <paramref name="str"/> repeated
|
||||
/// <paramref name="count"/> times.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="str"/> is <see langword="null"/>.</exception>
|
||||
/// <returns>
|
||||
/// Returns a <see cref="string" /> whose value is <paramref name="str" /> repeated
|
||||
/// <paramref name="count" /> times.
|
||||
/// </returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="str" /> is <see langword="null" />.</exception>
|
||||
public static string Repeat(this string str, int count)
|
||||
{
|
||||
if (str is null)
|
||||
@ -222,22 +238,22 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shuffles the characters in the string.
|
||||
/// Shuffles the characters in the string.
|
||||
/// </summary>
|
||||
/// <param name="str">The string to shuffle.</param>
|
||||
/// <returns>Returns a <see cref="string"/> containing the characters in <paramref name="str"/>, rearranged.</returns>
|
||||
/// <returns>Returns a <see cref="string" /> containing the characters in <paramref name="str" />, rearranged.</returns>
|
||||
public static string Shuffle(this string str)
|
||||
{
|
||||
return str.Shuffle(RandomExtensions.Random);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shuffles the characters in the string.
|
||||
/// Shuffles the characters in the string.
|
||||
/// </summary>
|
||||
/// <param name="str">The string to shuffle.</param>
|
||||
/// <param name="random">The <see cref="System.Random"/> instance.</param>
|
||||
/// <returns>Returns a <see cref="string"/> containing the characters in <paramref name="str"/>, rearranged.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="str"/> is <see langword="null"/>.</exception>
|
||||
/// <param name="random">The <see cref="System.Random" /> instance.</param>
|
||||
/// <returns>Returns a <see cref="string" /> containing the characters in <paramref name="str" />, rearranged.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="str" /> is <see langword="null" />.</exception>
|
||||
public static string Shuffle(this string str, Random random)
|
||||
{
|
||||
if (str is null)
|
||||
@ -249,13 +265,15 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Splits the <see cref="string"/> into chunks that are no greater than <paramref name="chunkSize"/> in length.
|
||||
/// Splits the <see cref="string" /> into chunks that are no greater than <paramref name="chunkSize" /> in length.
|
||||
/// </summary>
|
||||
/// <param name="str">The string to split.</param>
|
||||
/// <param name="chunkSize">The maximum length of each string in the returned result.</param>
|
||||
/// <returns>Returns an <see cref="IEnumerable{T}"/> containing <see cref="string"/> instances which are no
|
||||
/// greater than <paramref name="chunkSize"/> in length.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="str"/> is <see langword="null"/>.</exception>
|
||||
/// <returns>
|
||||
/// Returns an <see cref="IEnumerable{T}" /> containing <see cref="string" /> instances which are no
|
||||
/// greater than <paramref name="chunkSize" /> in length.
|
||||
/// </returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="str" /> is <see langword="null" />.</exception>
|
||||
public static IEnumerable<string> Split(this string str, int chunkSize)
|
||||
{
|
||||
if (str is null)
|
||||
@ -275,11 +293,11 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="string"/> to a <see cref="SecureString"/>.
|
||||
/// Converts a <see cref="string" /> to a <see cref="SecureString" />.
|
||||
/// </summary>
|
||||
/// <param name="str">The string to convert.</param>
|
||||
/// <returns>Returns a <see cref="SecureString"/>.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="str"/> is <see langword="null"/>.</exception>
|
||||
/// <returns>Returns a <see cref="SecureString" />.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="str" /> is <see langword="null" />.</exception>
|
||||
public static SecureString ToSecureString(this string str)
|
||||
{
|
||||
if (str is null)
|
||||
@ -302,12 +320,12 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="SecureString"/> to a <see cref="string"/>.
|
||||
/// Converts a <see cref="SecureString" /> to a <see cref="string" />.
|
||||
/// </summary>
|
||||
/// <param name="str">The <see cref="SecureString"/> to convert.</param>
|
||||
/// <param name="str">The <see cref="SecureString" /> to convert.</param>
|
||||
/// <param name="extension">Whether or not to use this extension method.</param>
|
||||
/// <returns>Returns a <see cref="string"/>.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="str"/> is <see langword="null"/>.</exception>
|
||||
/// <returns>Returns a <see cref="string" />.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="str" /> is <see langword="null" />.</exception>
|
||||
public static string ToString(this SecureString str, bool extension)
|
||||
{
|
||||
if (str is null)
|
||||
@ -319,12 +337,12 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses a shorthand time span string (e.g. 3w 2d 1.5h) and converts it to an instance of
|
||||
/// <see cref="TimeSpan"/>.
|
||||
/// Parses a shorthand time span string (e.g. 3w 2d 1.5h) and converts it to an instance of
|
||||
/// <see cref="TimeSpan" />.
|
||||
/// </summary>
|
||||
/// <param name="str">The input string.</param>
|
||||
/// <returns>Returns an instance of <see cref="TimeSpan"/>.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="str"/> is <see langword="null"/>.</exception>
|
||||
/// <returns>Returns an instance of <see cref="TimeSpan" />.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="str" /> is <see langword="null" />.</exception>
|
||||
public static TimeSpan ToTimeSpan(this string str)
|
||||
{
|
||||
if (str is null)
|
||||
|
@ -3,18 +3,20 @@
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for <see langword="struct"/> types.
|
||||
/// Extension methods for <see langword="struct" /> types.
|
||||
/// </summary>
|
||||
public static class StructExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns the next value in an <see cref="Enum"/> using the specified value as a starting point.
|
||||
/// Returns the next value in an <see cref="Enum" /> using the specified value as a starting point.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">An <see cref="Enum"/>.</typeparam>
|
||||
/// <param name="src">An <see cref="Enum"/> value.</param>
|
||||
/// <param name="wrap">Optional. Whether or not to wrap to the to the start of the enum. Defaults to
|
||||
/// true.</param>
|
||||
/// <returns>Returns a <see cref="T"/> value.</returns>
|
||||
/// <typeparam name="T">An <see cref="Enum" />.</typeparam>
|
||||
/// <param name="src">An <see cref="Enum" /> value.</param>
|
||||
/// <param name="wrap">
|
||||
/// Optional. Whether or not to wrap to the to the start of the enum. Defaults to
|
||||
/// true.
|
||||
/// </param>
|
||||
/// <returns>Returns a <see cref="T" /> value.</returns>
|
||||
public static T Next<T>(this T src, bool wrap = true)
|
||||
where T : struct
|
||||
{
|
||||
@ -29,13 +31,15 @@
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the previous value in an <see cref="Enum"/> using the specified value as a starting point.
|
||||
/// Returns the previous value in an <see cref="Enum" /> using the specified value as a starting point.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">An <see cref="Enum"/>.</typeparam>
|
||||
/// <param name="src">An <see cref="Enum"/> value.</param>
|
||||
/// <param name="wrap">Optional. Whether or not to wrap to the to the end of the enum. Defaults to
|
||||
/// true.</param>
|
||||
/// <returns>Returns a <see cref="T"/> value.</returns>
|
||||
/// <typeparam name="T">An <see cref="Enum" />.</typeparam>
|
||||
/// <param name="src">An <see cref="Enum" /> value.</param>
|
||||
/// <param name="wrap">
|
||||
/// Optional. Whether or not to wrap to the to the end of the enum. Defaults to
|
||||
/// true.
|
||||
/// </param>
|
||||
/// <returns>Returns a <see cref="T" /> value.</returns>
|
||||
public static T Previous<T>(this T src, bool wrap = true)
|
||||
where T : struct
|
||||
{
|
||||
|
@ -5,17 +5,17 @@
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a class which contains a <see cref="string"/> parser which converts into <see cref="TimeSpan"/>.
|
||||
/// Represents a class which contains a <see cref="string" /> parser which converts into <see cref="TimeSpan" />.
|
||||
/// </summary>
|
||||
public static class TimeSpanParser
|
||||
{
|
||||
/// <summary>
|
||||
/// Parses a shorthand time span string (e.g. 3w 2d 1.5h) and converts it to an instance of
|
||||
/// <see cref="TimeSpan"/>.
|
||||
/// Parses a shorthand time span string (e.g. 3w 2d 1.5h) and converts it to an instance of
|
||||
/// <see cref="TimeSpan" />.
|
||||
/// </summary>
|
||||
/// <param name="input">The input string.</param>
|
||||
/// <param name="provider">The format provider.</param>
|
||||
/// <returns>Returns an instance of <see cref="TimeSpan"/>.</returns>
|
||||
/// <returns>Returns an instance of <see cref="TimeSpan" />.</returns>
|
||||
public static TimeSpan Parse(string input, IFormatProvider provider = null)
|
||||
{
|
||||
const string realNumberPattern = @"([0-9]*\.[0-9]+|[0-9]+)";
|
||||
@ -26,7 +26,7 @@
|
||||
$"(?:{realNumberPattern} *s)? *" +
|
||||
$"(?:{realNumberPattern} *ms)?$";
|
||||
|
||||
Match match = Regex.Match(input, pattern);
|
||||
var match = Regex.Match(input, pattern);
|
||||
double weeks = 0, days = 0, hours = 0, minutes = 0, seconds = 0, milliseconds = 0;
|
||||
|
||||
if (match.Groups[1].Success)
|
||||
@ -62,7 +62,7 @@
|
||||
Trace.WriteLine($"Input: {input}");
|
||||
Trace.WriteLine($"Parsed: {weeks}w {days}d {hours}h {minutes}m {seconds}s {milliseconds}ms");
|
||||
|
||||
TimeSpan span = TimeSpan.Zero;
|
||||
var span = TimeSpan.Zero;
|
||||
|
||||
span += TimeSpan.FromDays(weeks * 7);
|
||||
span += TimeSpan.FromDays(days);
|
||||
|
Loading…
Reference in New Issue
Block a user