AlexStocks commented on code in PR #39:
URL: https://github.com/apache/dubbo-net/pull/39#discussion_r4534958440


##########
Compatibility/StringCompatibility.cs:
##########
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System;
+using System.Collections.Generic;
+
+namespace DubboNet.Compatibility
+{
+    internal static class StringCompatibility
+    {
+#if NETSTANDARD2_0
+        internal static bool Contains(this string value, string search, 
StringComparison comparison) =>
+            value.IndexOf(search, comparison) >= 0;
+        internal static bool Contains(this string value, char search, 
StringComparison comparison) =>
+            value.IndexOf(search.ToString(), comparison) >= 0;
+        internal static string[] Split(this string value, string separator, 
StringSplitOptions options = StringSplitOptions.None) =>
+            value.Split(new[] { separator }, options);
+        internal static string[] Split(this string value, char separator, 
StringSplitOptions options) =>
+            value.Split(new[] { separator }, options);
+        internal static string[] Split(this string value, char separator, int 
count) =>
+            value.Split(new[] { separator }, count);
+#endif
+        internal static string[] SplitTrimmed(string value, string separator, 
int count = int.MaxValue)
+        {
+#if NETSTANDARD2_0
+            // Standard lacks TrimEntries. Trim BEFORE testing emptiness and 
don't
+            // consume the result limit for blank entries; preserve the final 
remainder.
+            if (count < 0) throw new 
ArgumentOutOfRangeException(nameof(count));
+            if (count == 0) return Array.Empty<string>();
+            var parts = new List<string>();
+            int start = 0;
+            while (start <= value.Length)
+            {
+                if (parts.Count == count - 1)
+                {
+                    string remainder = value.Substring(start).Trim();
+                    if (remainder.Length != 0) parts.Add(remainder);
+                    break;
+                }
+                int end = value.IndexOf(separator, start, 
StringComparison.Ordinal);
+                string part = (end < 0 ? value.Substring(start) : 
value.Substring(start, end - start)).Trim();
+                if (part.Length != 0) parts.Add(part);
+                if (end < 0) break;
+                start = end + separator.Length;
+                // Remove blank entries even when the result limit was just 
reached.
+                if (parts.Count == count - 1)

Review Comment:
   [P2] `SplitTrimmed` 的 netstandard2.0 分支与 net8/net10 的 BCL `Split` 对 `count` 
的语义不一致,同一输入会解析出不同结果
   
   触发条件:`count` 已给定,且待切分文本在达到上限之前含有空段。netstandard2.0 分支刻意不让空段占用 `count`(第 40-41 
行注释、第 59-68 行的跳空循环),达到 `count-1` 后把剩余部分整体作为最后一段;net8/net10 分支直接调用 
`value.Split(new[]{separator}, count, TrimEntries | RemoveEmptyEntries)`(第 72 
行),BCL 中空段**会**占用 `count`,达到上限后的剩余内容整体保留(含其中的分隔符)。
   
   实际影响:生产调用点 `DubboService/DataModle/DubboInfo/DubboStatusInfo.cs` 的 
`GetKeyValuePairs` 用 `SplitTrimmed(part.Trim(), MESVAL_SPLIT, 2)`(`MESVAL_SPLIT 
= ":"`)解析 Telnet `status` 返回的键值行,并对 `keyValue.Length != 2` 记告警。输入 
`"status::ACTIVE"`:
   - net8/net10:`["status", ":ACTIVE"]` —— 值保留前导冒号;
   - netstandard2.0:`["status", "ACTIVE"]` —— 前导冒号被跳空逻辑吃掉。
   同一份服务端响应,在 .NET Framework / netstandard2.0 消费方与 net8/net10 消费方得到不同的字段值;而本 PR 
的目标正是让这三条目标框架行为一致。
   
   证据:`Compatibility/StringCompatibility.cs:40-41`(注释声明"不占用结果上限")、`:48` 与 
`:59-68`(自定义计数与跳空)、`:72`(非 Standard 分支走 BCL)。差异方向由两段实现直接决定,与 `TrimEntries` 
是否生效无关。说明:本机无 .NET 运行时(`dotnet`/`mono` 均不存在),未能实测 BCL 侧输出,上述基于 `Split(String[], 
Int32, StringSplitOptions)` 的公开语义推导,属本轮验证缺口。
   
   修复方向:统一成一种语义。要么让 netstandard2.0 分支先按 `count` 上限切分(空段计入上限,最后一段保留剩余内容)再做 
Trim/RemoveEmpty,与 BCL 完全对齐;要么两端都走同一个自实现。建议补一个参数化用例覆盖 `count` + 空段(如 `("a::b", 
":", 2)`),并在 netstandard2.0 与 net8/net10 上分别断言相同结果。
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to