Skip to content

Commit 0a0eed0

Browse files
committed
format
1 parent b76f42a commit 0a0eed0

File tree

12 files changed

+375
-108
lines changed

12 files changed

+375
-108
lines changed

.editorconfig

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
# 如果要从更高级别的目录继承 .editorconfig 设置,请删除以下行
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
end_of_line = crlf
7+
insert_final_newline = true
8+
9+
# CA2208: Instantiate argument exceptions correctly
10+
dotnet_diagnostic.CA2208.severity = none
11+
dotnet_diagnostic.CS8305.severity = none
12+
13+
[*.cs]
14+
15+
#### Core EditorConfig 选项 ####
16+
17+
# 缩进和间距
18+
indent_size = 4
19+
indent_style = space
20+
tab_width = 4
21+
22+
#### .NET 编码约定 ####
23+
24+
# 组织 Using
25+
dotnet_separate_import_directive_groups = false
26+
dotnet_sort_system_directives_first = true
27+
file_header_template = unset
28+
29+
# this. 和 Me. 首选项
30+
dotnet_style_qualification_for_event = false:suggestion
31+
dotnet_style_qualification_for_field = false:suggestion
32+
dotnet_style_qualification_for_method = false:suggestion
33+
dotnet_style_qualification_for_property = false:suggestion
34+
35+
# 语言关键字与 BCL 类型首选项
36+
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
37+
dotnet_style_predefined_type_for_member_access = true:suggestion
38+
39+
# 括号首选项
40+
dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity
41+
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity
42+
dotnet_style_parentheses_in_other_operators = never_if_unnecessary
43+
dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity
44+
45+
# 修饰符首选项
46+
dotnet_style_require_accessibility_modifiers = for_non_interface_members
47+
48+
# 表达式级首选项
49+
dotnet_style_object_initializer = true:suggestion
50+
dotnet_style_collection_initializer = true:suggestion
51+
dotnet_style_explicit_tuple_names = true:suggestion
52+
dotnet_style_null_propagation = true:suggestion
53+
dotnet_style_coalesce_expression = true:suggestion
54+
dotnet_style_prefer_inferred_tuple_names = true:suggestion
55+
dotnet_style_operator_placement_when_wrapping = beginning_of_line
56+
dotnet_style_prefer_auto_properties = true:suggestion
57+
dotnet_style_prefer_compound_assignment = true:suggestion
58+
dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
59+
dotnet_style_prefer_conditional_expression_over_return = true:suggestion
60+
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
61+
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
62+
dotnet_style_prefer_simplified_boolean_expressions = true:suggestion
63+
dotnet_style_prefer_simplified_interpolation = true:suggestion
64+
65+
# 字段首选项
66+
dotnet_style_readonly_field = true
67+
68+
# 参数首选项
69+
dotnet_code_quality_unused_parameters = all
70+
71+
#### C# 编码约定 ####
72+
73+
# var 首选项
74+
csharp_style_var_elsewhere = true:suggestion
75+
csharp_style_var_for_built_in_types = true:suggestion
76+
csharp_style_var_when_type_is_apparent = true:suggestion
77+
78+
# Expression-bodied 成员
79+
csharp_style_expression_bodied_accessors = true:suggestion
80+
# csharp_style_expression_bodied_constructors = true:suggestion
81+
csharp_style_expression_bodied_indexers = true:suggestion
82+
csharp_style_expression_bodied_lambdas = true:suggestion
83+
csharp_style_expression_bodied_local_functions = true:suggestion
84+
# csharp_style_expression_bodied_methods = true:suggestion
85+
# IDE0022: 使用表达式主体来表示方法
86+
csharp_style_expression_bodied_methods = when_on_single_line
87+
csharp_style_expression_bodied_operators = true:suggestion
88+
csharp_style_expression_bodied_properties = true:suggestion
89+
90+
# 模式匹配首选项
91+
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
92+
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
93+
csharp_style_prefer_not_pattern = true:suggestion
94+
csharp_style_prefer_pattern_matching = true:suggestion
95+
csharp_style_prefer_switch_expression = true:suggestion
96+
97+
# null 检查首选项
98+
csharp_style_conditional_delegate_call = true:suggestion
99+
100+
# 修饰符首选项
101+
csharp_prefer_static_local_function = true:suggestion
102+
csharp_preferred_modifier_order = public, private, protected, internal, static, extern, new, virtual, abstract, sealed, override, readonly, unsafe, volatile, async
103+
104+
# 代码块首选项
105+
csharp_prefer_braces = false:silent
106+
csharp_prefer_simple_using_statement = true:suggestion
107+
108+
# 表达式级首选项
109+
csharp_prefer_simple_default_expression = true:suggestion
110+
csharp_style_deconstructed_variable_declaration = true:suggestion
111+
csharp_style_inlined_variable_declaration = true:suggestion
112+
csharp_style_prefer_index_operator = true:suggestion
113+
csharp_style_prefer_range_operator = true:suggestion
114+
csharp_style_throw_expression = true:suggestion
115+
csharp_style_unused_value_assignment_preference = discard_variable:suggestion
116+
csharp_style_unused_value_expression_statement_preference = discard_variable:suggestion
117+
csharp_style_pattern_local_over_anonymous_function = true:suggestion
118+
119+
# using 指令首选项
120+
csharp_using_directive_placement = outside_namespace:suggestion
121+
122+
#### C# 格式规则 ####
123+
124+
# 新行首选项
125+
csharp_new_line_before_catch = true
126+
csharp_new_line_before_else = true
127+
csharp_new_line_before_finally = true
128+
csharp_new_line_before_members_in_anonymous_types = true
129+
csharp_new_line_before_members_in_object_initializers = true
130+
csharp_new_line_before_open_brace = all
131+
csharp_new_line_between_query_expression_clauses = true
132+
133+
# 缩进首选项
134+
csharp_indent_block_contents = true
135+
csharp_indent_braces = false
136+
csharp_indent_case_contents = true
137+
csharp_indent_case_contents_when_block = false
138+
csharp_indent_labels = one_less_than_current
139+
csharp_indent_switch_labels = true
140+
141+
# 空格键首选项
142+
csharp_space_after_cast = false
143+
csharp_space_after_colon_in_inheritance_clause = true
144+
csharp_space_after_comma = true
145+
csharp_space_after_dot = false
146+
csharp_space_after_keywords_in_control_flow_statements = true
147+
csharp_space_after_semicolon_in_for_statement = true
148+
csharp_space_around_binary_operators = before_and_after
149+
csharp_space_around_declaration_statements = false
150+
csharp_space_before_colon_in_inheritance_clause = true
151+
csharp_space_before_comma = false
152+
csharp_space_before_dot = false
153+
csharp_space_before_open_square_brackets = false
154+
csharp_space_before_semicolon_in_for_statement = false
155+
csharp_space_between_empty_square_brackets = false
156+
csharp_space_between_method_call_empty_parameter_list_parentheses = false
157+
csharp_space_between_method_call_name_and_opening_parenthesis = false
158+
csharp_space_between_method_call_parameter_list_parentheses = false
159+
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
160+
csharp_space_between_method_declaration_name_and_open_parenthesis = false
161+
csharp_space_between_method_declaration_parameter_list_parentheses = false
162+
csharp_space_between_parentheses = false
163+
csharp_space_between_square_brackets = false
164+
165+
# 包装首选项
166+
csharp_preserve_single_line_blocks = true
167+
csharp_preserve_single_line_statements = true
168+
169+
#### 命名样式 ####
170+
171+
# 命名规则
172+
173+
dotnet_naming_rule.interface_should_be_begins_with_i.severity = suggestion
174+
dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface
175+
dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i
176+
177+
dotnet_naming_rule.types_should_be_pascal_case.severity = suggestion
178+
dotnet_naming_rule.types_should_be_pascal_case.symbols = types
179+
dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case
180+
181+
dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = suggestion
182+
dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members
183+
dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case
184+
185+
dotnet_naming_rule.visible_field_should_be_pascal_case.severity = suggestion
186+
dotnet_naming_rule.visible_field_should_be_pascal_case.symbols = visible_field
187+
dotnet_naming_rule.visible_field_should_be_pascal_case.style = pascal_case
188+
189+
dotnet_naming_rule.const_field_should_be_pascal_case.severity = suggestion
190+
dotnet_naming_rule.const_field_should_be_pascal_case.symbols = const_field
191+
dotnet_naming_rule.const_field_should_be_pascal_case.style = pascal_case
192+
193+
dotnet_naming_rule.private_static_field_should_be_pascal_begin_with__.severity = suggestion
194+
dotnet_naming_rule.private_static_field_should_be_pascal_begin_with__.symbols = private_static_field
195+
dotnet_naming_rule.private_static_field_should_be_pascal_begin_with__.style = pascal_begin_with__
196+
197+
dotnet_naming_rule.private_static_readonly_field_should_be_pascal_begin_with__.severity = suggestion
198+
dotnet_naming_rule.private_static_readonly_field_should_be_pascal_begin_with__.symbols = private_static_readonly_field
199+
dotnet_naming_rule.private_static_readonly_field_should_be_pascal_begin_with__.style = pascal_begin_with__
200+
201+
dotnet_naming_rule.private_field_should_be_camel_begin_with__.severity = suggestion
202+
dotnet_naming_rule.private_field_should_be_camel_begin_with__.symbols = private_field
203+
dotnet_naming_rule.private_field_should_be_camel_begin_with__.style = camel_begin_with__
204+
205+
# 符号规范
206+
207+
dotnet_naming_symbols.interface.applicable_kinds = interface
208+
dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
209+
dotnet_naming_symbols.interface.required_modifiers =
210+
211+
dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum
212+
dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
213+
dotnet_naming_symbols.types.required_modifiers =
214+
215+
dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method
216+
dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
217+
dotnet_naming_symbols.non_field_members.required_modifiers =
218+
219+
dotnet_naming_symbols.const_field.applicable_kinds = field
220+
dotnet_naming_symbols.const_field.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
221+
dotnet_naming_symbols.const_field.required_modifiers = const
222+
223+
dotnet_naming_symbols.visible_field.applicable_kinds = field
224+
dotnet_naming_symbols.visible_field.applicable_accessibilities = public, internal, protected_internal
225+
dotnet_naming_symbols.visible_field.required_modifiers =
226+
227+
dotnet_naming_symbols.private_field.applicable_kinds = field
228+
dotnet_naming_symbols.private_field.applicable_accessibilities = private
229+
dotnet_naming_symbols.private_field.required_modifiers =
230+
231+
dotnet_naming_symbols.private_static_field.applicable_kinds = field
232+
dotnet_naming_symbols.private_static_field.applicable_accessibilities = private
233+
dotnet_naming_symbols.private_static_field.required_modifiers = static
234+
235+
dotnet_naming_symbols.private_static_readonly_field.applicable_kinds = field
236+
dotnet_naming_symbols.private_static_readonly_field.applicable_accessibilities = private
237+
dotnet_naming_symbols.private_static_readonly_field.required_modifiers = readonly, static
238+
239+
# 命名样式
240+
241+
dotnet_naming_style.pascal_case.required_prefix =
242+
dotnet_naming_style.pascal_case.required_suffix =
243+
dotnet_naming_style.pascal_case.word_separator =
244+
dotnet_naming_style.pascal_case.capitalization = pascal_case
245+
246+
dotnet_naming_style.begins_with_i.required_prefix = I
247+
dotnet_naming_style.begins_with_i.required_suffix =
248+
dotnet_naming_style.begins_with_i.word_separator =
249+
dotnet_naming_style.begins_with_i.capitalization = pascal_case
250+
251+
dotnet_naming_style.camel_begin_with__.required_prefix = _
252+
dotnet_naming_style.camel_begin_with__.required_suffix =
253+
dotnet_naming_style.camel_begin_with__.word_separator =
254+
dotnet_naming_style.camel_begin_with__.capitalization = camel_case
255+
256+
dotnet_naming_style.pascal_begin_with__.required_prefix = _
257+
dotnet_naming_style.pascal_begin_with__.required_suffix =
258+
dotnet_naming_style.pascal_begin_with__.word_separator =
259+
dotnet_naming_style.pascal_begin_with__.capitalization = pascal_case
260+
261+
# ReSharper properties
262+
resharper_max_initializer_elements_on_line = 1

Pixeval.Extensions.Translators.Baidu/Client/BaiduTranslatorClient.cs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
//https://github.com/sangyuxiaowu/Sang.Baidu.TranslateAPI/blob/main/Sang.Baidu.TranslateAPI/BaiduTranslator.cs
1+
//https://github.com/sangyuxiaowu/Sang.Baidu.TranslateAPI/blob/main/Sang.Baidu.TranslateAPI/BaiduTranslator.cs
2+
#nullable disable
23

3-
using System.Net.Http.Json;
44
using System.Text.Json;
55
using System.Text.Json.Serialization;
66
using System.Text;
77
using System.Security.Cryptography;
88
using System.Web;
99

1010
namespace Pixeval.Extensions.Translators.Baidu.Client;
11-
#nullable disable
11+
1212
/// <summary>
1313
/// 文档:https://fanyi-api.baidu.com/doc/21
1414
/// </summary>
@@ -92,8 +92,6 @@ public async Task<BaiduTranslateResult> Translate(string text, string toLanguage
9292
/// <param name="text">请求翻译文本,长度控制在 6000 bytes以内(汉字约为输入参数 2000 个)</param>
9393
/// <param name="toLanguage">翻译目标语言</param>
9494
/// <param name="fromLanguage">翻译源语言,可为auto,自动检测</param>
95-
/// <param name="salt">设置后将使用传入的随机数</param>
96-
/// <param name="sign">设置后将使用传入的签名结果</param>
9795
/// <returns></returns>
9896
public async Task<BaiduTranslateResult> Translate(string text, string toLanguage, string fromLanguage = "auto")
9997
{
@@ -185,14 +183,13 @@ public void Dispose()
185183
}
186184

187185
}
186+
188187
[JsonSerializable(typeof(BaiduTranslateResult))]
189188
[JsonSerializable(typeof(List<TransResult>))]
190189
[JsonSerializable(typeof(TransResult))]
191190
[JsonSerializable(typeof(bool))]
192191
[JsonSerializable(typeof(string))]
193-
internal partial class SourceGenerationContext : JsonSerializerContext
194-
{
195-
}
192+
internal partial class SourceGenerationContext : JsonSerializerContext;
196193

197194
public class BaiduTranslateResult
198195
{
@@ -233,7 +230,7 @@ public class BaiduTranslateResult
233230
/// <returns></returns>
234231
public string GetResult()
235232
{
236-
return Trans_Result != null && Trans_Result.Count > 0 ? Trans_Result[0].Dst : null;
233+
return Trans_Result is { Count: > 0 } ? Trans_Result[0].Dst : null;
237234
}
238235

239236
}
@@ -254,6 +251,7 @@ public class TransResult
254251
[JsonPropertyName("dst")]
255252
public string Dst { get; set; }
256253
}
254+
257255
public static class Cryptography
258256
{
259257

@@ -266,12 +264,9 @@ public static class Cryptography
266264
/// <returns>计算结果</returns>
267265
public static string MD5(this string str, bool isUpper = false, bool isBig = true)
268266
{
269-
using (var md5 = System.Security.Cryptography.MD5.Create())
270-
{
271-
var result = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
272-
var strResult = isBig ? BitConverter.ToString(result) : BitConverter.ToString(result, 4, 8);
273-
return isUpper ? strResult.Replace("-", "") : strResult.Replace("-", "").ToLower();
274-
}
267+
var result = System.Security.Cryptography.MD5.HashData(Encoding.UTF8.GetBytes(str));
268+
var strResult = isBig ? BitConverter.ToString(result) : BitConverter.ToString(result, 4, 8);
269+
return isUpper ? strResult.Replace("-", "") : strResult.Replace("-", "").ToLower();
275270
}
276271

277272
/// <summary>
@@ -282,12 +277,8 @@ public static string MD5(this string str, bool isUpper = false, bool isBig = tru
282277
/// <returns>计算结果</returns>
283278
public static string SHA1(this string str, bool isUpper = false)
284279
{
285-
using (var sha1 = System.Security.Cryptography.SHA1.Create())
286-
{
287-
var result = sha1.ComputeHash(Encoding.UTF8.GetBytes(str));
288-
var strResult = BitConverter.ToString(result);
289-
return isUpper ? strResult.Replace("-", "") : strResult.Replace("-", "").ToLower();
290-
}
280+
var result = System.Security.Cryptography.SHA1.HashData(Encoding.UTF8.GetBytes(str));
281+
var strResult = BitConverter.ToString(result);
282+
return isUpper ? strResult.Replace("-", "") : strResult.Replace("-", "").ToLower();
291283
}
292-
293-
}
284+
}

0 commit comments

Comments
 (0)