NightOwl888 / ICU4N

International Components for Unicode for .NET
Apache License 2.0
26 stars 7 forks source link

Task: Modify methods to use ref or out parameters instead of arrays parameters and return values, where sensible #57

Closed NightOwl888 closed 11 months ago

NightOwl888 commented 11 months ago

Java does not support ref or out parameters, so as a workaround methods were designed to accept or return array types of specific lengths (each element representing a specific value). These methods need to be analyzed and changed to use ref or out parameters, where appropriate.

Example

        /// <summary>
        /// Parse a single non-whitespace character '<paramref name="ch"/>', optionally
        /// preceded by whitespace.
        /// </summary>
        /// <param name="id">The string to be parsed.</param>
        /// <param name="pos">INPUT-OUTPUT parameter.  On input, pos[0] is the
        /// offset of the first character to be parsed.  On output, pos[0]
        /// is the index after the last parsed character.  If the parse
        /// fails, pos[0] will be unchanged.</param>
        /// <param name="ch">The non-whitespace character to be parsed.</param>
        /// <returns>true if '<paramref name="ch"/>' is seen preceded by zero or more
        /// whitespace characters.</returns>
        public static bool ParseChar(string id, int[] pos, char ch)
        {
            int start = pos[0];
            pos[0] = PatternProps.SkipWhiteSpace(id, pos[0]);
            if (pos[0] == id.Length ||
                    id[pos[0]] != ch)
            {
                pos[0] = start;
                return false;
            }
            ++pos[0];
            return true;
        }

Can be changed to:

        /// <summary>
        /// Parse a single non-whitespace character '<paramref name="ch"/>', optionally
        /// preceded by whitespace.
        /// </summary>
        /// <param name="id">The string to be parsed.</param>
        /// <param name="pos">INPUT-OUTPUT parameter.  On input, pos is the
        /// offset of the first character to be parsed.  On output, pos
        /// is the index after the last parsed character.  If the parse
        /// fails, pos will be unchanged.</param>
        /// <param name="ch">The non-whitespace character to be parsed.</param>
        /// <returns>true if '<paramref name="ch"/>' is seen preceded by zero or more
        /// whitespace characters.</returns>
        public static bool ParseChar(string id, ref int pos, char ch)
        {
            int start = pos;
            pos = PatternProps.SkipWhiteSpace(id, pos);
            if (pos == id.Length ||
                    id[pos] != ch)
            {
                pos = start;
                return false;
            }
            ++pos;
            return true;
        }

Be sure to update the documentation appropriately to reflect the changes.

Of course, for this example to compile, all callers of ParseChar() as well as the PatternProps.SkipWhiteSpace() method will need to be modified, as well.

To find the offending methods, I have created code analyzers in the lucenenet-codeanalysis-dev Visual Studio extension. Just install the extension and filter for the following:

  1. LuceneDev1003 - Finds all methods that accept an array parameter (except for char[])
  2. LuceneDev1004 - Finds all methods that return an array parameter (except for char[])