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


##########
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:
   @AlexStocks  已经review了这段实现,当前实现在当前场景是正确的。就是有意“刻意不让空段占用 
count”,因为后面.NET10的版本使用的是return value.Split(new[] { separator }, 
count,StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);  
也是指定RemoveEmptyEntries也是会移除空段段。(也就是说net8/net10返回的也将是["status", "ACTIVE"])
   
   实测结果
   在当前 PR 代码上实测:
   输入,count=2 | .NET 8 原生 | .NET 10 原生 | Standard 实现
   -- | -- | -- | --
   status::ACTIVE | ["status","ACTIVE"] | 相同 | 相同
   a::b | ["a","b"] | 相同 | 相同
   a::b:c | ["a","b:c"] | 相同 | 相同
   
   实际生产方法 GetKeyValuePairs 也验证过,得到的是 status = ACTIVE。
   关键是我们传入了 RemoveEmptyEntries:被忽略的空项不占返回结果的名额;BCL 
在拼接最后一项前,也有跳过空项的处理。这与我们的兼容实现一致。[.NET 
官方源码](https://github.com/dotnet/runtime/blob/v8.0.15/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs)
   评论中的 ["status", ":ACTIVE"],在不使用 RemoveEmptyEntries 时才会出现。
   为了预防描述中的问题,我已经新增了几个针对性的单元测试到测试工程里(已经更新到pr里了)。
   
   



-- 
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