Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for string.LastIndexOf(char, int) and string.LastIndexOf(string, int) should search toward the beginning of the string #400 #845

Merged
merged 3 commits into from
Sep 10, 2018

Conversation

MatthiasJentsch
Copy link
Contributor

@MatthiasJentsch MatthiasJentsch commented Sep 7, 2018

Description

Found that searching backward in a string was not implemented in Library_corlib_native_System_String::IndexOf. I've added the ability to search also backward in a string. That's needed for the string.LastIndexOf and string.LastIndexOfAny methods. To support the backward search in an UTF8 string I've also implemended the bool CLR_RT_UnicodeHelper::MoveBackwardInUTF8( const char* utf8StringStart, int iMaxChars ) function.

Motivation and Context

How Has This Been Tested?

namespace NFApp1
{
	public class Program
	{
		public static void Main()
		{
			string s1 = "nanoFramework tests are running on nanoCLR!";

			int i01 = s1.LastIndexOf('t');													// 17
			int i02 = s1.LastIndexOf('x');                                                  // -1

			int i03 = s1.LastIndexOf("nano");                                               // 35
			int i04 = s1.LastIndexOf("micro");                                              //-1

			int i05 = s1.LastIndexOf('t', 15);												// 14
			int i06 = s1.LastIndexOf('t', 13);												// -1

			int i07 = s1.LastIndexOf("nano", 10);											// 0
			int i08 = s1.LastIndexOf("nano", 40);											// 35
			int i09 = s1.LastIndexOf("nano", 3);                                            // 0
			int i10 = s1.LastIndexOf("nano", 2);                                            // -1
			int i11 = s1.LastIndexOf("are", 10);                                            // -1

			int i12 = s1.LastIndexOf('n', 20, 20);                                          // 2
			int i13 = s1.LastIndexOf('n', 40, 10);                                          // 37
			int i14 = s1.LastIndexOf('w', 5, 3);                                            // -1

			int i15 = s1.LastIndexOf("nano", 40, 10);										// 35
			int i16 = s1.LastIndexOf("nano", 25, 10);                                       // -1
			int i17 = s1.LastIndexOf("nano", 3, 3);                                         // -1

			int i18 = s1.LastIndexOfAny(new char[] { 'e', 's', 't', 'l' });					// 22
			int i19 = s1.LastIndexOfAny(new char[] { 'b', 'c', 'd', 'f', 'h', 'j' });		// -1

			int i20 = s1.LastIndexOfAny(new char[] { 'e', 'r', 'n', 's', 't', 'l' }, 10);   // 8
			int i21 = s1.LastIndexOfAny(new char[] { 'e', 'r', 'n', 's', 't', 'l' }, 38);	// 37
			int i22 = s1.LastIndexOfAny(new char[] { 'e', 'r', 's', 't', 'l' }, 4);			// -1

			int i23 = s1.LastIndexOfAny(new char[] { 'e', 'n', 's', 't', 'l' }, 20, 10);	// 18
			int i24 = s1.LastIndexOfAny(new char[] { 'e', 'n', 's', 't', 'l' }, 12, 4);		// -1
			int i25 = s1.LastIndexOfAny(new char[] { 'e', 's', 't', 'l' }, 5, 3);			// -1

			string s2 = "nanoFramework tests are running!";

			int i26 = s2.IndexOf('t');														// 14
			int i27 = s2.IndexOf('x');                                                      // -1

			int i28 = s2.IndexOf("est");                                                    // 15
			int i29 = s2.IndexOf("micro");                                                  // -1

			int i30 = s2.IndexOf('t', 15);													// 17
			int i31 = s2.IndexOf('t', 19);													// -1

			string s3 = "nanoFramework tests are running on nanoCLR!";

			int i32 = s3.IndexOf("nano", 10);												// 35
			int i33 = s3.IndexOf("nano", 36);                                               // -1
			int i34 = s3.IndexOf("nano", 42);												// -1

			int i35 = s3.IndexOf('C', 30, 10);												// 39
			int i36 = s3.IndexOf('C', 25, 10);												// -1
			int i37 = s3.IndexOf('C', 40, 3);												// -1
			int i38 = s3.IndexOf("nano", 30, 10);                                           // 35

			int i39 = s3.IndexOf("nano", 25, 10);											// -1
			int i40 = s3.IndexOf("nano", 40, 3);											// -1

			int i41 = s3.IndexOfAny(new char[] { 'e', 's', 't', 'l' });						// 8
			int i42 = s3.IndexOfAny(new char[] { 'b', 'c', 'd', 'f', 'h', 'j' });			// -1

			int i43 = s3.IndexOfAny(new char[] { 'e', 'r', 'n', 's', 't', 'l' }, 10);		// 11
			int i44 = s3.IndexOfAny(new char[] { 'e', 'r', 'n', 's', 't', 'l' }, 38);		// -1
			int i45 = s3.IndexOfAny(new char[] { 'e', 'r', 'n', 's', 't', 'l' }, 42);		// -1

			int i46 = s3.IndexOfAny(new char[] { 'e', 'n', 's', 't', 'l' }, 9, 10);			// 14
			int i47 = s3.IndexOfAny(new char[] { 'e', 'n', 's', 't', 'l' }, 9, 4);			// -1
			int i48 = s3.IndexOfAny(new char[] { 'e', 'n', 's', 't', 'l' }, 40, 3);			// -1
		}
	}
}

The values at the end of the lines are the expected values. All calls give now the expected values.

Types of changes

  • Improvement (non-breaking change that improves a feature, code or algorithm)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Signed-off-by: Matthias Jentsch info@matthias-jentsch.de

…f(string, int) should search toward the beginning of the string
…f(string, int) should search toward the beginning of the string

Signed-off-by: Matthias Jentsch <info@matthias-jentsch.de>
@nfbot
Copy link
Member

nfbot commented Sep 7, 2018

Hi @MatthiasJentsch,

I'm nanoFramework bot.
Thank you for your contribution!

A human will be reviewing it shortly. 😉

Copy link
Member

@josesimoes josesimoes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very good! Excellent job. 💯 😄

@MatthiasJentsch MatthiasJentsch merged commit d738ea0 into nanoframework:develop Sep 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Common libs Everything related with common libraries Type: bug Type: enhancement
Projects
None yet
3 participants