Hot answers tagged

18

You should take a look at the possibility to name groups in regex patterns; your match pattern can then be a oneliner: const string pattern = @"^(?<name>\w+)\[(?<index>\d+)\]$"; Match match = Regex.Match(command[2], pattern); if (match.Success) { string featureName = match.Groups["name"].Value; int index = int.Parse(match....


12

One possible place you can improve performance is here: const string pattern = @"\[(.*?)\]"; var query = command[2]; var matches = Regex.Matches(query, pattern); //Gets anything inside the brackets index = Convert.ToInt32(matches[0].Groups[1].Value); //should be an int featureName = command[2].Substring(0, command[2].IndexOf('[')).ToUpper(); You're ...


12

These declarations are not prototypes: void getDataFromRapidSrcFile(); int getTotalPositions(); These declare functions that can be called with any number of arguments. It appears that they should take no arguments; we indicate that like this: void getDataFromRapidSrcFile(void); int getTotalPositions(void); It's a good idea to make the same change ...


7

Why would you want to use Regular Expressions anyway if the base format of the string is always the same? Name[Number] seems like an easy pattern. Just iterate through the characters one by one and store all characters in the first string until you reach the first bracket. Then store the numbers in the second string (until you reach the closing bracket). ...


6

Functions should be very short: as a maximum, 2 or 3 screens (considering a screen size of 24 lines), but much less if possible; and shouldn't indent more than 2 or 3 levels of indentation normally. You should try to break that big fat function into a lot of small functions.


5

Regarding: char* speed = malloc(sizeof(char) * 6); The expression sizeof(char) is defined in the C standard as 1. Multiplying anything by 1 has no effect and just clutters the code, making it more difficult to understand, debug, etc. When calling any of the heap allocation functions: malloc() calloc() realloc(), always check (!=NULL) the returned value ...


4

You have a memory leak. Well several. All of the static char * functions allocate memory using malloc() which is never freed. Now let's take a look at how they are used, for example strcpy(sweepSpeed, getSweepSpeed(currentLine, i)); So getSweepSpeed() is returning a pointer to a string, which is immediately copied into another string sweepSpeed, then the ...


4

Hash all string constants I'm presuming you only have a limited number of keywords ("move", "vertex" etc.). Hash all of those with something fast - CRC32 is perfectly adequate. Split the string into space-separated tokens, as usual, and calculate the hash of the first and second tokens. Then you just need to compare the calculated hash with the hashes of ...


3

What a wall of text! Also known as write-only code, or job security. You want to write understandable code, that you can come back to in 6 months, and within 5 minutes understand enough to change it, if necessary. This has a for-loop over a sorted list comprehension, with embedded if-then-else, of a zip of two sorted json queries, with a lambda thrown in ...


3

The first obvious optimization would be to not call Regex.Matches(string, string) which compiles the regular expression every single time. Compiling the regex is an expensive operation. Instead, create a Regex (which compiles the expression exactly once) and keep it around during your several thousand invocations. That should by itself do the trick (because,...


3

API Calling code has to pass input to CommandLineParser's constructor, but do the actual parsing with ParseAll. Calling ParseAll a second time then returns an empty output. A static CommandLineParser.Parse(input) method that creates that instance internally would be more sensible. It's not clear what syntax this parser supports. Both "/?" and "--file C:\...


3

You can directly isolate and extract the content type by matching the start of the line, the label and a space, then restart the fullstring match, then match one or more non-newline characters. Pattern Demo: https://regex101.com/r/QZz9IE/1/ PHP: (Demo) echo preg_match('~^Content-Type: \K.+~m', $Data, $match) ? $match[0] : 'fail'; Output: application/...


3

I'll start the serious bug, then work inwards from main(). Biggest bug The biggest problem with the code is that it only reads one command from each connection, leaving any subsequent commands for the next client to execute. Even if we read more commands, by looping until handle() returns false before closing the client socket, this still won't help if ...


3

This post on Stack Overflow explains why you are getting the wrong years. https://stackoverflow.com/questions/37766353/pandas-to-datetime-parsing-wrong-year Based on your code all of the two digit years in your data set will be converted to 19XX years. The only problem I can see is that if your data set includes dates across both centuries ( 19XX and 20XX) ...


2

Review You are re-inventing the wheel here. Building a custom API for transforming templates is very hard to maintain. You have a for each loop now, but soon you'll need much more language constructs. Save yourself the pain and use T4 instead. boiler-plate string-based code constructs are hard to maintain concatenating to a string is bad practice for ...


2

Follow-up In your previous post, I described some design issues I found. I'm happy to see your new design is cleaner (specially the lexer) and no longer depends on an already parsed array of tokens! Pieter Witvoet has already went through your code and detected many edge cases your API falls short. (No need for me to re-iterate them) This is mainly ...


2

Bug GetDBDate does not return a date, it return a date time; either change the method name if indeed you wanted a date time or return result.Date; Issues code is not optimized because intermediate casting to string is required; this is especially unfortunate when the specified value could have been casted directly to the specified type code is not ...


Only top voted, non community-wiki answers of a minimum length are eligible