Subscribe: The Daily WTF
Added By: Feedage Forager Feedage Grade C rated
Language: English
apos apos  apos l\\  apos  ini  l\\ apos  l\\  match  preg match  regular expressions  regular  relationship  replace  system  values 
Rate this Feed
Rate this feedRate this feedRate this feedRate this feedRate this feed
Rate this feed 1 starRate this feed 2 starRate this feed 3 starRate this feed 4 starRate this feed 5 star

Comments (0)

Feed Details and Statistics Feed Statistics
Preview: The Daily WTF

The Daily WTF

Curious Perversions in Information Technology

Last Build Date: Mon, 24 Oct 2016 18:21:34 GMT


CodeSOD: Keeping it Regular

Mon, 24 Oct 2016 10:30:00 GMT

Regular expressions are like one of those multi-tools: they're a knife, they're a screwdriver, they're pliers, and there's a pair of tweezers stuck in the handle. We can use them to do anything. For example, Linda inherited a site that counted up and down votes, like Reddit, implemented in CoffeeScript. Instead of using variables or extracting the text from the DOM, this code brought regular expressions to bear. updateBookmarkCounter = (upOrDown) -> counterSpan = $('.bookmark .counter') spanHtml = counterSpan.html() count = spanHtml.match(/\d/).first().toInteger() newCount = if (upOrDown == 'down') then (count - 1) else (count + 1) newCount = 0 if newCount < 1 counterSpan.html(spanHtml.replace(/\d/, newCount)) updateUserBookmarkCount upOrDown There’s a glitch in this code, and no, it’s not that this code exists in the first place. Think about what happens when the number of votes exceeds 10. Okay, maybe not the best use of regexes. What about sanitizing inputs? That seems like a textbook use case. Alexander T’s co-worker found a very clever way to convert any input into a floating point number. Any input. function convertToFloat(value) { if(typeof value == "number") return value; return parseFloat(value.replace(/\D/g, '')); } Values like “D12.3” convert seamlessly to “123”. You know what else regexes can do? Parse things! Not just HTML and XML, but anything. Like, for example, parsing out an INI file. Kate found this. $ini = array(); preg_match_all('/\[(?[^\]\r\n]+)\][\s\r\n]*(?([^\r\n\[=]+(=[^\r\n]+)?[\s\r\n]*)+)?/i', file_get_contents($iniFile), $ini); foreach($ini['sections'] as $i=>$section) { $ini[$section] = array(); $values = $sections['values'][$i]; preg_match_all('/[\s]*(?[^\r\n\=]+)(=(?[^\r\n]+))?[\r\n]*/i',$values,$ini); foreach($ini['names'] as $j=>$name) { $name = trim($name); if($name && !preg_match("/[#;]/", $name)){ $value = trim($ini['values'][$j]); if(!preg_match("/[#;]/", $value)) $ini[$section][$name] = $value; } } } She was able to replace that entire block with $ini = parse_ini_file($iniFile, true);. parse_ini_file is a built-in library function in PHP. Speaking of parsing, R.J. L. works for a company that runs printed documents through optical character recognition, and then uses regular expressions to identify interesting parts of the document to store in the database. These regular expressions are written in a custom, in-house regex language, that is almost but not quite a PCRE. By using their own regular expression language, tasks that might be inelegant or complicated in traditional languages become simple. For example, this regex find the document identifier on any page. ([:-.,;/\\(]{0,2}(( [C|c][P|p][K,<|k,<][0-9]{11} )||([:#.$",'#-/|][C|c][P|p][K,<|k,<][0-9]{11} )||( [C|c][P|p][K,<|k,<][0-9]{11}[ :.$",'#-/|l\\])||([:.$",'#-/|][C|c][P|p][K,<|k,<][0-9]{11}[:.$",'#-/|l\\])||( 01[A|a|C|c|D|d|E|e|R|r][0-9]{7} )||([:#.$",'#-/|]01[A|a|C|c|D|d|E|e|R|r][0-9]{7} )||(01[A|a|C|c|D|d|E|e|R|r][0-9]{7}[:#.$",'#-/|l\\])||([:#.$",'#-/|]01[A|a|C|c|D |d|E|e|R|r][0-9]{7}[:#.$",'#-/|l\\])||( 02[A|a|B|b|C|c|D|d|E|e|F|f][0-9]{7} )||([:#.$",'#-/|]02[A|a|B|b|C|c|D|d|E|e|F|f][0-9]{7} )||( 02[A|a|B|b|C|c|D|d|E|e |F|f][0-9]{7}[:#.$",'#-/|l\\])||([:#-/|]02[A|a|B|b|C|c|D|d|E|e|F|f][0-9]{7}[:#.$ ",'#-/|l\\])||( 04[C|c|D|d|F|f|V|v][0-9]{7} )||([:#.$",'#-/|]04[C|c|D|d|F|f|V|v][0-9]{7} )||( 04[C|c|D|d|F|f|V|v][0-9]{7}[:# .$",'#-/|l\\])||([:#.$",'#-/|]04[C|c|D|d|F|f|V|v][0-9]{7}[:#.$",'#-/|l\\])||( 05[M|m|A|a][0-9]{7} )||([:#.$",'#-/|]05[M|m|A|a][0-9]{7} )||( 05[M|m|A|a][0-9]{7 }[:#.$&quo[...]


Tue, 04 Oct 2016 10:30:00 GMT

Given the rise of the internet in the mid 1990's, various events and companies led up to Adobe releasing Flash. Not to be out done, in the mid noughts, Microsoft created their own version called Silverlight. Somewhere down the road, Facebook, Instagram and others put forth React. These can sit on top of a webservice, like, for example, WCF to make it easier for web-facing programs to call home to interact with back-end applications to do useful things like display videos of cats being, well, cats. Occasionally, folks even attempt to use these tools to provide access to business applications. Some time back, Fred became a hired-gun/consultant/architect to a small financial company to help them replace a dying 150K LOC Silverlight UI with a React front-end, and the underlying WCF API (named Rest.Services for some reason). This allegedly trivial task was budgeted to take three months. Ten months down the road, Silverlight and the underlying code base were way ahead on points while the battle raged on. Eventually, management acquiesced and allowed the entire UI to be rewritten from scratch. The back-end, however... The application was a financial budgeting/reporting set-up. While it served its purpose from a business perspective, under the hood was a Cluster-O-WTF™ that simply would not cooperate with any attempts at change. Additionally, over time, the system became more and more obstinate about providing answers to queries. One morning, Fred noticed that the overnight updates were still running and the CPUs were pegged at 100%. A quick spelunk into the SQL showed why. Like every other financial system, there were multiple tables containing related data. One might expect each table to have a PK and the relationship-table to have a bunch of FKs. One would be wrong. Fred found multiple sets of tables, each with the following design pattern. create table Aux_1 as ( pk_id bigint, field1 varchar(10), ... ); create table Aux_2 as ( pk_id bigint, field2 varchar(10), ... ); create table RelationshipAux1Aux2 as ( pk_id bigint, keys varchar2(300), ... ) In the keys field, there were delimited FK references to the other related tables. These were hand-crafted relationships in a relational database! The system would delete the old auxiliary data records and insert new ones, creating new corresponding relationship records in the process. Without any sort of referential integrity, the old relationship records would be orphaned. With no home-grown concept of cascading deletes to match this set-up, in several of the relationship tables, more than 18 million of 20 million rows were orphaned. Naturally, since the developers of this brillance didn't know to use FKs, they certainly didn't know to use built-in indices either. Of course, that meant that in order to find anything, the database needed to grind through and pick apart tens of millions of orphaned relationship rows in addition to the few million rows of actual data. After several years of this Garbage Accumulation™, it reached critical mass and the system just couldn't grind through enough data to finish. A careful delete got the system running again. Fred now spends his days adding indices and attempting to replace the home-grown FK mechanism with actual FKs. [Advertisement] Application Release Automation for DevOps – integrating with best of breed development tools. Free for teams with up to 5 users. Download and learn more today! [...]