< Summary - Jellyfin

Information
Class: Jellyfin.Extensions.StringExtensions
Assembly: Jellyfin.Extensions
File(s): /srv/git/jellyfin/src/Jellyfin.Extensions/StringExtensions.cs
Line coverage
59%
Covered lines: 22
Uncovered lines: 15
Coverable lines: 37
Total lines: 127
Line coverage: 59.4%
Branch coverage
88%
Covered branches: 16
Total branches: 18
Branch coverage: 88.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%210%
RemoveDiacritics(...)100%11100%
HasDiacritics(...)100%22100%
Count(...)100%44100%
LeftPart(...)100%44100%
RightPart(...)100%66100%
Transliterated(...)0%620%

File(s)

/srv/git/jellyfin/src/Jellyfin.Extensions/StringExtensions.cs

#LineLine coverage
 1using System;
 2using System.Text.RegularExpressions;
 3using ICU4N.Text;
 4
 5namespace Jellyfin.Extensions
 6{
 7    /// <summary>
 8    /// Provides extensions methods for <see cref="string" />.
 9    /// </summary>
 10    public static partial class StringExtensions
 11    {
 012        private static readonly Lazy<string> _transliteratorId = new(() =>
 013            Environment.GetEnvironmentVariable("JELLYFIN_TRANSLITERATOR_ID")
 014            ?? "Any-Latin; Latin-Ascii; Lower; NFD; [:Nonspacing Mark:] Remove; [:Punctuation:] Remove;");
 15
 016        private static readonly Lazy<Transliterator?> _transliterator = new(() =>
 017        {
 018            try
 019            {
 020                return Transliterator.GetInstance(_transliteratorId.Value);
 021            }
 022            catch (ArgumentException)
 023            {
 024                return null;
 025            }
 026        });
 27
 28        // Matches non-conforming unicode chars
 29        // https://mnaoumov.wordpress.com/2014/06/14/stripping-invalid-characters-from-utf-16-strings/
 30
 31        [GeneratedRegex("([\ud800-\udbff](?![\udc00-\udfff]))|((?<![\ud800-\udbff])[\udc00-\udfff])|(�)")]
 32        private static partial Regex NonConformingUnicodeRegex();
 33
 34        /// <summary>
 35        /// Removes the diacritics character from the strings.
 36        /// </summary>
 37        /// <param name="text">The string to act on.</param>
 38        /// <returns>The string without diacritics character.</returns>
 39        public static string RemoveDiacritics(this string text)
 19640            => Diacritics.Extensions.StringExtensions.RemoveDiacritics(
 19641                NonConformingUnicodeRegex().Replace(text, string.Empty));
 42
 43        /// <summary>
 44        /// Checks whether or not the specified string has diacritics in it.
 45        /// </summary>
 46        /// <param name="text">The string to check.</param>
 47        /// <returns>True if the string has diacritics, false otherwise.</returns>
 48        public static bool HasDiacritics(this string text)
 1249            => Diacritics.Extensions.StringExtensions.HasDiacritics(text)
 1250                || NonConformingUnicodeRegex().IsMatch(text);
 51
 52        /// <summary>
 53        /// Counts the number of occurrences of [needle] in the string.
 54        /// </summary>
 55        /// <param name="value">The haystack to search in.</param>
 56        /// <param name="needle">The character to search for.</param>
 57        /// <returns>The number of occurrences of the [needle] character.</returns>
 58        public static int Count(this ReadOnlySpan<char> value, char needle)
 59        {
 860            var count = 0;
 861            var length = value.Length;
 155462            for (var i = 0; i < length; i++)
 63            {
 76964                if (value[i] == needle)
 65                {
 1466                    count++;
 67                }
 68            }
 69
 870            return count;
 71        }
 72
 73        /// <summary>
 74        /// Returns the part on the left of the <c>needle</c>.
 75        /// </summary>
 76        /// <param name="haystack">The string to seek.</param>
 77        /// <param name="needle">The needle to find.</param>
 78        /// <returns>The part left of the <paramref name="needle" />.</returns>
 79        public static ReadOnlySpan<char> LeftPart(this ReadOnlySpan<char> haystack, char needle)
 80        {
 37481            if (haystack.IsEmpty)
 82            {
 283                return ReadOnlySpan<char>.Empty;
 84            }
 85
 37286            var pos = haystack.IndexOf(needle);
 37287            return pos == -1 ? haystack : haystack[..pos];
 88        }
 89
 90        /// <summary>
 91        /// Returns the part on the right of the <c>needle</c>.
 92        /// </summary>
 93        /// <param name="haystack">The string to seek.</param>
 94        /// <param name="needle">The needle to find.</param>
 95        /// <returns>The part right of the <paramref name="needle" />.</returns>
 96        public static ReadOnlySpan<char> RightPart(this ReadOnlySpan<char> haystack, char needle)
 97        {
 1098            if (haystack.IsEmpty)
 99            {
 1100                return ReadOnlySpan<char>.Empty;
 101            }
 102
 9103            var pos = haystack.LastIndexOf(needle);
 9104            if (pos == -1)
 105            {
 1106                return haystack;
 107            }
 108
 8109            if (pos == haystack.Length - 1)
 110            {
 3111                return ReadOnlySpan<char>.Empty;
 112            }
 113
 5114            return haystack[(pos + 1)..];
 115        }
 116
 117        /// <summary>
 118        /// Returns a transliterated string which only contain ascii characters.
 119        /// </summary>
 120        /// <param name="text">The string to act on.</param>
 121        /// <returns>The transliterated string.</returns>
 122        public static string Transliterated(this string text)
 123        {
 0124            return (_transliterator.Value is null) ? text : _transliterator.Value.Transliterate(text);
 125        }
 126    }
 127}