SPO600 – Improving STRCSPN, The Implementation

This is an update for the last time I posted. Currently I have a working version of the new implementation, that I think is ready to be code checked. In this post I want to explain the implementation and what it is suppose to do, and in the next post after this I will post my testing method as well as test scripts.

So after adjustments and trial and error this is the current iteration of my adjusted function :


Let’s break this down.

First thing up we create a bitmap with maximum number of unsigned char capacity. We will need this in order to store the values we have in the string we want to find a match in.

Next we set up  a few variables that we will use, n being the return value and parity is our bit indicator.

We will  initially populate the entire matchmap array with 0 using memset. This allows us to ensure we will not mistakenly match with some weird value.

while(*map) loop iterators over the characters we are looking for in the string, and for every character we cast it into an unsigned char which lets us use it as a array indexer in our matchmap. Once we have the index we place the value 1 in the position. This is for later to allow us compare the two strings.

matchmap[0] = parity; This statement is so the null terminator never matches.

while(matchmap[(unsigned char)*s++] ^ parity). This is the part that does the work. We use the original string we are searching in and again converting it to an unsigned char to get the index value after which we use the bitwise operator ^ XOR to see if it already had the value set to one, when we initially went over the look up string. If values do not match we simply add one to N and move on to the next value, once a match is found the loop terminates and we get our final result.

This is current iteration of my implementation that I think is ready for code check.

As a side note, the original function is a mess and if anything this just helps it look nicer.

To find the tests that I ran here will be the blogpost : https://andreybykin.wordpress.com/2017/04/09/spo600-testing-strcspn-improvement/


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s