Author Topic: Way to simplify SwapChar  (Read 581 times)

I've made a function that swaps the positions of two characters in a string.
It works fine, but I want to know if there's any way to simplify it.

Here's the code:
Code: [Select]
function swapChar(%input, %p1, %p2)
{
%pt1 = %p1; %pt2 = %p2;
%p1 = Min(%pt1, %pt2);
%p2 = Max(%pt1, %pt2);

%c1 = getSubStr(%input, %p1, 1);
%c2 = getSubStr(%input, %p2, 1);

%b1 = %p1 > 0 ? getSubStr(%input, 0, %p1) : "";
%b2 = %p1+1 != %p2 ? getSubStr(%input, %p1+1, %p2-1) : "";
%f = %p2 < strLen(%input)-1 ? getSubStr(%input, %p2+1, strLen(%input)) : "";
return %b1 @ %c2 @ %b2 @ %c1 @ %f;
}

assuming %p1 and %p2 are integers that represent the position of the character

function swapchar(%str, %a, %b)
{
   if(%a > %b)
   {
      %b = %temp;
      %b = %a;
      %a = %temp;
   }
   return getsubstr(%str, 0, %a-1) @ getsubstr(%str, %b, 1) @ getsubstr(%str, %a+1, %b-%a-1) @ getsubstr(%str, %a, 1) @ getsubstr(%str, %b+1, strlen(%str));
}

although of course this is reliant on good integer input.  I probably wouldn't even put in the check to make sure %b is larger than %a except it looks like you did that in yours and I imagine you would call me out on it.

assuming %p1 and %p2 are integers that represent the position of the character

function swapchar(%str, %a, %b)
{
   if(%a > %b)
   {
      %b = %temp;
      %b = %a;
      %a = %temp;
   }
   return getsubstr(%str, 0, %a-1) @ getsubstr(%str, %b, 1) @ getsubstr(%str, %a+1, %b-%a-1) @ getsubstr(%str, %a, 1) @ getsubstr(%str, %b+1, strlen(%str));
}

although of course this is reliant on good integer input.  I probably wouldn't even put in the check to make sure %b is larger than %a except it looks like you did that in yours and I imagine you would call me out on it.
Shouldn't %b = %temp; be %temp = %b;?


assuming %p1 and %p2 are integers that represent the position of the character

function swapchar(%str, %a, %b)
{
   if(%a > %b)
   {
      %b = %temp;
      %b = %a;
      %a = %temp;
   }
   return getsubstr(%str, 0, %a-1) @ getsubstr(%str, %b, 1) @ getsubstr(%str, %a+1, %b-%a-1) @ getsubstr(%str, %a, 1) @ getsubstr(%str, %b+1, strlen(%str));
}

although of course this is reliant on good integer input.  I probably wouldn't even put in the check to make sure %b is larger than %a except it looks like you did that in yours and I imagine you would call me out on it.
What if %a or %b is 0, and what if they are the same? %b-%a-1 will be -1, and if %a or %b is 0 (meaning the first charachter), it will also try to getsubstr with -1!

What if %a or %b is 0, and what if they are the same? %b-%a-1 will be -1, and if %a or %b is 0 (meaning the first charachter), it will also try to getsubstr with -1!
Not everything has to compensate for human stupidity. Chances are, if you're using the function you probably know how to use it correctly. The most optimized code is Nexus' without the min/max check.

Shouldn't %b = %temp; be %temp = %b;?

er yea whoops

What if %a or %b is 0, and what if they are the same? %b-%a-1 will be -1, and if %a or %b is 0 (meaning the first charachter), it will also try to getsubstr with -1!

yea saw that coming a mile away.  As I said I wouldn't even have the which is higher check if I didn't think you would jump on it right away.

Er actually I wonder what would happen if %a is 0 and it does a getsubstr starting from -1 to 0
hopefully it will return empty but I have been surprised by these things before
« Last Edit: May 06, 2012, 08:51:42 PM by Nexus »

er yea whoops

yea saw that coming a mile away.  As I said I wouldn't even have the which is higher check if I didn't think you would jump on it right away.

Er actually I wonder what would happen if %a is 0 and it does a getsubstr starting from -1 to 0
hopefully it will return empty but I have been surprised by these things before
It'll probably result in a console error. getSubStr is verbose as hell with its errors.

So then there are 3 chances for errors I see here:
if a is 0 (first charachter)
if b is the length of the string minus one (last charachter)
or if a is 1 less than b.

So what's the fastest way to check for all those things, and make adjustments as nessescary?