Solved

Regex Converting C# to C

Posted on 2011-03-16
4
324 Views
Last Modified: 2012-05-11
I have a small function to compress a string so it is easy to transmit.

Basically long sequences of similar characters are substituted so for example.

0000000 11111 00000000000 22222222222222222222

becomes (ignore spaces)

A7A B5B A11A C20C

The C# is:

     static string CompressMessage(string message)
     {
         return Regex.Replace(message, @"([0-7])\1{3,}", (Match m) => String.Format("{0}{1}{0}", (char)(m.Groups[1].Value[0] + 17), m.Value.Length));
     }

Question:

I there an equivalent C library OR how easy is it to write this in C (for an embedded system)



0
Comment
Question by:oddszone
  • 3
4 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35149703
There is the Boost regex library, but I don't know if it can be loaded to an embedded system.

Here is my stab at a C version. It's probably sloppy, but it should give you a start, I hope!


Yes, my C-Fu is not strong  ; )
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char* compressMessage(char* message);

void main(void)
{
    char source[] = "0000000 11111 00000000000 22222222222222222222";
    char* result;
    
    printf("Source string: %s\n", source);
    
    result = compressMessage(source);
    
    printf("Compressed string: %s\n", result);
    
    free(result);
}

char* compressMessage(char* message)
{
    // There's probably a better way to create the buffer
    char* buffer = (char*)malloc(sizeof(char) * strlen(message));
    int curIdx = 0;
    int bufIdx = 0;
    
    while (message[curIdx] != '\0')                         // While not eos (would be better if length was a function parameter
    {
        char curChr;
        int curCnt = 1;
        
        curChr = message[curIdx++];
        
        while (message[curIdx++] == curChr) { curCnt++; }   // Loop over repeating characters
        
        curIdx--;                                           // Readjust indexer so we don't drop any characters from output
        
        if (curChr == ' ')                                  // Print spaces in output
        {
            int i;
            
            for (i = 0; i < curCnt; i++)
            {
                buffer[bufIdx++] = ' ';
            }
        }
        else                                                // Print encoded string to output
        {
            bufIdx = sprintf(&buffer[bufIdx], "%c%d%c", curChr + ('A' - '0'), curCnt, curChr + ('A' - '0'));
        }
    }
    
    buffer[bufIdx] = '\0';                                  // Terminate the output string
    
    return buffer;
}

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 35149716
Line 50 should read (plus-equals):
bufIdx += sprintf(&buffer[bufIdx], "%c%d%c", curChr + ('A' - '0'), curCnt, curChr + ('A' - '0'));

Open in new window

0
 

Author Comment

by:oddszone
ID: 35150988
Thats fantastic. I just need to modify so it only substitutes when >3 chars in a row detected otherwise you add more characters to the output which defeats the object.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35151401
Ahhh, yes. I missed that little point, huh?  Glad you can work with it and glad to help  : )
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Both in life and business – not all partnerships are created equal. Spend 30 short minutes with us to learn:   • Key questions to ask when considering a partnership to accelerate your business into the cloud • Pitfalls and mistakes other partners…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now