Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | DFS <nospam@dfs.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Challenge: tightest code to find-replace a string |
| Date | 2014-06-07 18:30 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <ln03q7$htt$1@dont-email.me> (permalink) |
| References | <lmrer6$4l3$1@dont-email.me> <0.55d78fa3ca96ce6aed82.20140606144139BST.87r432w3xo.fsf@bsb.me.uk> |
On 06/06/2014 09:41 AM, Ben Bacarisse wrote:
> DFS <nospam@dfs.com> writes:
>
>> * reads an existing file
>> * writes changes to new file
>> * counts replacements made by line
>> * counts total replacements made
>> * no fancy usage of sed!
>
> It reports rather than counts these matches. I would never write a
> function with this spec. because it destroys its usefulness in other
> contexts. A function should do one thing well.
So are you talking about a separate function to count the number of
potential replacements before making them?
I've only ever seen find-replace functions report how many replacements
were actually made, after the fact.
> I'd write a string match/replace function that returns the number of
> matches. If I needed the counts reported by line, I'd write a wrapper
> that adds those.
>
> int replace_string(const char *match, const char *repl, int stopper,
> FILE *fi, FILE *fo)
> {
> int nmatches = 0, c;
> const char *mp = match;
> while ((c = fgetc(fi)) != EOF && c != stopper)
> if (c == *mp) {
> if (!*++mp) {
> ++nmatches;
> fputs(repl, fo);
> }
> }
> else {
> mp = match;
> fputc(c, fo);
> }
> return nmatches;
> }
>
> Called with stopper == EOF it processes a whole file. Note how removing
> the line buffer actually simplifies the code, whilst also removing an
> unnecessary restriction. It's not uncommon for this to happen (there
> was a recent thread about this).
>
> Called with stopper == '\n' it processes a line and so this wrapper
> prints the report:
>
> void replace_string_report(const char *match, const char *repl,
> FILE *fi, FILE *fo)
> {
> int total_matches = 0, lineno = 0;
> while (!feof(fi)) {
> int nm = replace_string(match, repl, '\n', fi, fo);
> printf("\nLine %d: %d replacements\n", ++lineno, nm);
> total_matches += nm;
> }
> printf("%d replacements\n", total_matches);
> }
Above you said my original code "reports rather than counts these
matches." and "I would never write a function with this spec."
Maybe I'm confused, but it looks like you did exactly that. Your
program makes the replacements, then reports how many were made.
From your earlier statements, I was expecting a separate function to
count the number of matches.
> Here's the driver for testing.
>
> int main(int argc, char **argv)
> {
> if (argc > 2) {
> FILE *fin = argc > 3 ? fopen(argv[3], "r") : stdin;
> FILE *fout = argc > 4 ? fopen(argv[4], "w") : stdout;
> if (fin && fout)
> replace_string_report(argv[1], argv[2], fin, fout);
> }
> }
>
> Functions that mix tasks that can be logically separated are best
> avoided. Functions with hard-wired file names and strings are, well,
> let's just say, sub-optimal. Students used to say "but it's because I'm
> just testing" but a simple driver like the one above makes testing
> much easier than having the files and strings hard wired.
>
> <snip>
I definitely like the functionality in main() and the replace-string()
code, but there might be bugs:
original input data
replace 46 with ----
1: 14513111664214260256543011122553234523520226455552
2: 41602561064325541006060354620223361346535061545034
3: 63164621623130051346620535103421535300201464252314
4: 30013144611120401561305220534605456101542562311260
5: 30501506124251042546364005110661421500320026101445
6: 35355334213621124600100142264440253516210400362562
7: 65140560414014522562466550406113020500531011441421
8: 60543325410345553336424511333322104440166124450061
9: 44310321435636412163052026304311532342515351020026
10: 10536502643531635353214012163164121056142415600245
should return:
1: 14513111664214260256543011122553234523520226455552
2: 4160256106432554100606035----202233613----535061545034
3: 6316----216231300513----620535103421535300201----4252314
4: 3001314----1112040156130522053----05456101542562311260
5: 305015061242510425----364005110661421500320026101445
6: 3535533421362112----00100142264440253516210400362562
7: 65140560414014522562----6550406113020500531011441421
8: 60543325410345553336424511333322104440166124450061
9: 44310321435636412163052026304311532342515351020026
10: 10536502643531635353214012163164121056142415600245
* running your code with stopper EOF removes most occurrences of
standalone 4, but not all. Looks like repeating characters (eg 44)
aren't handled correctly, so it missed the first '46' in line 4, which
probably lead to it undercounting the replacements by one.
output:
1: 15131116621260256530111225532352352022655552
2: 1602561063255100606035----202233613----5350615503
3: 6316----216231300513----62053510321535300201----425231
4: 30013146111200156130522053----055610152562311260
5: 3050150612251025----360051106612150032002610145
6: 353553321362112----0010012264025351621000362562
7: 6510560101522562----6550061130205005310114121
8: 605332510355533362511333322104016612450061
9: 431032135636121630520263031153232515351020026
10: 10536502635316353532101216316121056121560025
* running it with EOF stopper reports all replacements were made in line 1
[dfs@home files]$ ./find_replace_BenB 46 ---- random.txt randomOut.txt
Line 1: 9 replacements
9 replacements
* running it with the '\n' stopper miscounts the lines, and removes the
line breaks.
[dfs@home files]$ ./find_replace_BenB 46 ---- random.txt randomOut.txt
Line 1: 0 replacements
Line 2: 2 replacements
Line 3: 3 replacements
Line 4: 1 replacements
Line 5: 1 replacements
Line 6: 1 replacements
Line 7: 1 replacements
Line 8: 0 replacements
Line 9: 0 replacements
Line 10: 0 replacements
Line 11: 0 replacements
9 replacements
output:
151311166212602565301112255323523520226555521602561063255100606035----202233613----53506155036316----216231300513----62053510321535300201----42523130013146111200156130522053----0556101525623112603050150612251025----360051106612150032002610145353553321362112----00100122640253516210003625626510560101522562----655006113020500531011412160533251035553336251133332210401661245006143103213563612163052026303115323251535102002610536502635316353532101216316121056121560025
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Challenge: tightest code to find-replace a string DFS <nospam@dfs.com> - 2014-06-06 00:08 -0400
Re: Challenge: tightest code to find-replace a string DFS <nospam@dfs.com> - 2014-06-06 04:34 -0400
Re: Challenge: tightest code to find-replace a string Jorgen Grahn <grahn+nntp@snipabacken.se> - 2014-06-06 10:04 +0000
Re: Challenge: tightest code to find-replace a string Mark Storkamp <mstorkamp@yahoo.com> - 2014-06-06 07:27 -0500
Re: Challenge: tightest code to find-replace a string Robert Wessel <robertwessel2@yahoo.com> - 2014-06-06 11:55 -0500
Re: Challenge: tightest code to find-replace a string Johannes Bauer <dfnsonfsduifb@gmx.de> - 2014-06-06 20:47 +0200
Re: Challenge: tightest code to find-replace a string Ike Naar <ike@iceland.freeshell.org> - 2014-06-06 06:05 +0000
Re: Challenge: tightest code to find-replace a string Noob <root@127.0.0.1> - 2014-06-06 10:17 +0200
Re: Challenge: tightest code to find-replace a string Ian Collins <ian-news@hotmail.com> - 2014-06-08 21:20 +1200
Re: Challenge: tightest code to find-replace a string Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-06-06 14:41 +0100
Re: Challenge: tightest code to find-replace a string "BartC" <bc@freeuk.com> - 2014-06-06 16:29 +0100
Re: Challenge: tightest code to find-replace a string Jorgen Grahn <grahn+nntp@snipabacken.se> - 2014-06-06 17:27 +0000
Re: Challenge: tightest code to find-replace a string Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-06-06 22:16 +0100
Re: Challenge: tightest code to find-replace a string "BartC" <bc@freeuk.com> - 2014-06-06 23:26 +0100
Re: Challenge: tightest code to find-replace a string Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-06-06 23:38 +0100
Re: Challenge: tightest code to find-replace a string Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-06-06 08:31 -0700
Re: Challenge: tightest code to find-replace a string Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-06-06 22:12 +0100
Re: Challenge: tightest code to find-replace a string Chad <cdalten@gmail.com> - 2014-06-07 12:19 -0700
Re: Challenge: tightest code to find-replace a string Chad <cdalten@gmail.com> - 2014-06-07 13:38 -0700
Re: Challenge: tightest code to find-replace a string raltbos@xs4all.nl (Richard Bos) - 2014-06-08 10:34 +0000
Re: Challenge: tightest code to find-replace a string DFS <nospam@dfs.com> - 2014-06-07 18:30 -0400
Re: Challenge: tightest code to find-replace a string Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-06-08 13:09 +0100
Re: Challenge: tightest code to find-replace a string DFS <nospam@dfs.com> - 2014-06-08 12:42 -0400
Re: Challenge: tightest code to find-replace a string Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-06-09 01:44 +0100
Re: Challenge: tightest code to find-replace a string Siri Crews <chine.bleu@yahoo.com> - 2014-06-08 22:15 -0700
Re: Challenge: tightest code to find-replace a string Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-06-09 12:58 +0100
Re: Challenge: tightest code to find-replace a string Ike Naar <ike@iceland.freeshell.org> - 2014-06-09 06:40 +0000
Re: Challenge: tightest code to find-replace a string Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-06-09 13:04 +0100
Re: Challenge: tightest code to find-replace a string "BartC" <bc@freeuk.com> - 2014-06-09 15:27 +0100
Re: Challenge: tightest code to find-replace a string Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-06-09 16:22 +0100
Re: Challenge: tightest code to find-replace a string "BartC" <bc@freeuk.com> - 2014-06-09 18:06 +0100
Re: Challenge: tightest code to find-replace a string "BartC" <bc@freeuk.com> - 2014-06-09 22:38 +0100
Re: Challenge: tightest code to find-replace a string Tim Rentsch <txr@alumni.caltech.edu> - 2014-06-10 00:57 -0700
Re: Challenge: tightest code to find-replace a string DFS <nospam@dfs.com> - 2014-06-07 17:36 -0400
Re: Challenge: tightest code to find-replace a string "BartC" <bc@freeuk.com> - 2014-06-08 09:57 +0100
Re: Challenge: tightest code to find-replace a string DFS <nospam@dfs.com> - 2014-06-08 12:43 -0400
Re: Challenge: tightest code to find-replace a string Keith Thompson <kst-u@mib.org> - 2014-06-08 15:27 -0700
Re: Challenge: tightest code to find-replace a string "BartC" <bc@freeuk.com> - 2014-06-09 10:02 +0100
Re: Challenge: tightest code to find-replace a string Keith Thompson <kst-u@mib.org> - 2014-06-09 08:03 -0700
Re: Challenge: tightest code to find-replace a string Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-06-08 04:43 -0700
Re: Challenge: tightest code to find-replace a string DFS <nospam@dfs.com> - 2014-06-08 13:26 -0400
Re: Challenge: tightest code to find-replace a string Keith Thompson <kst-u@mib.org> - 2014-06-08 15:24 -0700
Re: Challenge: tightest code to find-replace a string Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-06-09 08:39 -0700
Re: Challenge: tightest code to find-replace a string Keith Thompson <kst-u@mib.org> - 2014-06-09 08:57 -0700
Re: Challenge: tightest code to find-replace a string Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-06-09 09:52 -0700
Re: Challenge: tightest code to find-replace a string Keith Thompson <kst-u@mib.org> - 2014-06-09 11:25 -0700
Re: Challenge: tightest code to find-replace a string Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-06-09 13:42 -0700
Re: Challenge: tightest code to find-replace a string Keith Thompson <kst-u@mib.org> - 2014-06-09 15:02 -0700
Re: Challenge: tightest code to find-replace a string Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-06-09 16:04 -0700
Re: Challenge: tightest code to find-replace a string raltbos@xs4all.nl (Richard Bos) - 2014-06-10 10:23 +0000
Re: Challenge: tightest code to find-replace a string raltbos@xs4all.nl (Richard Bos) - 2014-06-09 16:57 +0000
Re: Challenge: tightest code to find-replace a string raltbos@xs4all.nl (Richard Bos) - 2014-06-10 10:06 +0000
Re: Challenge: tightest code to find-replace a string "Chris M. Thomasson" <no@spam.invalid> - 2014-06-09 12:43 -0700
Re: Challenge: tightest code to find-replace a string "Chris M. Thomasson" <no@spam.invalid> - 2014-06-09 14:32 -0700
Re: Challenge: tightest code to find-replace a string "Chris M. Thomasson" <no@spam.invalid> - 2014-06-09 16:09 -0700
Re: Challenge: tightest code to find-replace a string James Kuyper <jameskuyper@verizon.net> - 2014-06-09 21:35 -0400
Re: Challenge: tightest code to find-replace a string DFS <nospam@dfs.com> - 2014-06-10 09:39 -0400
Re: Challenge: tightest code to find-replace a string James Kuyper <jameskuyper@verizon.net> - 2014-06-10 11:26 -0400
Re: Challenge: tightest code to find-replace a string raltbos@xs4all.nl (Richard Bos) - 2014-06-11 16:16 +0000
Re: Challenge: tightest code to find-replace a string Keith Thompson <kst-u@mib.org> - 2014-06-11 09:25 -0700
Re: Challenge: tightest code to find-replace a string Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-06-11 13:48 -0700
Re: Challenge: tightest code to find-replace a string "BartC" <bc@freeuk.com> - 2014-06-11 22:04 +0100
Re: Challenge: tightest code to find-replace a string Keith Thompson <kst-u@mib.org> - 2014-06-11 14:59 -0700
Re: Challenge: tightest code to find-replace a string Ike Naar <ike@iceland.freeshell.org> - 2014-06-11 21:35 +0000
Re: Challenge: tightest code to find-replace a string Keith Thompson <kst-u@mib.org> - 2014-06-11 14:56 -0700
Re: Challenge: tightest code to find-replace a string Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-06-12 02:24 -0700
Re: Challenge: tightest code to find-replace a string Keith Thompson <kst-u@mib.org> - 2014-06-12 09:09 -0700
Re: Challenge: tightest code to find-replace a string Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-06-13 00:40 -0700
Re: Challenge: tightest code to find-replace a string "BartC" <bc@freeuk.com> - 2014-06-13 09:17 +0100
Re: Challenge: tightest code to find-replace a string "Chris M. Thomasson" <no@spam.invalid> - 2014-06-14 13:12 -0700
csiph-web