Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: elastic/elasticsearch-net
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 8.14.7
Choose a base ref
...
head repository: elastic/elasticsearch-net
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 8.14.8
Choose a head ref
  • 1 commit
  • 1 file changed
  • 2 contributors

Commits on Jul 30, 2024

  1. Improve ToCamelCase to match STJ behavior (#8278) (#8280)

    Co-authored-by: Florian Bernd <[email protected]>
    github-actions[bot] and flobernd authored Jul 30, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    411aaee View commit details
Showing with 46 additions and 9 deletions.
  1. +46 −9 src/Elastic.Clients.Elasticsearch.Shared/Core/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

using System;

#if ELASTICSEARCH_SERVERLESS
namespace Elastic.Clients.Elasticsearch.Serverless;
#else
@@ -10,18 +12,53 @@ namespace Elastic.Clients.Elasticsearch;

internal static class StringExtensions
{
internal static string ToCamelCase(this string s)
// Taken from:
// https://github.com/dotnet/runtime/blob/main/src/libraries/System.Text.Json/Common/JsonCamelCaseNamingPolicy.cs

internal static string ToCamelCase(this string name)
{
if (string.IsNullOrEmpty(s))
return s;
if (string.IsNullOrEmpty(name) || !char.IsUpper(name[0]))
{
return name;
}

#if NET
return string.Create(name.Length, name, (chars, name) =>
{
name.CopyTo(chars);
FixCasing(chars);
});
#else
var chars = name.ToCharArray();
FixCasing(chars);
return new string(chars);
#endif
}

private static void FixCasing(Span<char> chars)
{
for (var i = 0; i < chars.Length; i++)
{
if (i == 1 && !char.IsUpper(chars[i]))
{
break;
}

var hasNext = (i + 1 < chars.Length);

if (!char.IsUpper(s[0]))
return s;
// Stop when next char is already lowercase.
if (i > 0 && hasNext && !char.IsUpper(chars[i + 1]))
{
// If the next char is a space, lowercase current char before exiting.
if (chars[i + 1] == ' ')
{
chars[i] = char.ToLowerInvariant(chars[i]);
}

var camelCase = char.ToLowerInvariant(s[0]).ToString();
if (s.Length > 1)
camelCase += s.Substring(1);
break;
}

return camelCase;
chars[i] = char.ToLowerInvariant(chars[i]);
}
}
}