diff --git a/btn/README.md b/btn/README.md index f02e6f5..7e88d64 100644 --- a/btn/README.md +++ b/btn/README.md @@ -9,7 +9,8 @@ This program prints a long string having no white-space characters, given a text where N = the number of characters before a new-line character; - its range: 1 ~ 32, + if N = 0, no new-line character is inserted, + its range: 0 ~ 32, TEXT_FILE_NAME = the file name to be input-redirected. ``` @@ -31,4 +32,4 @@ python btn.py N < TEXT_FILE_NAME ``` ghc buntang.hs -``` \ No newline at end of file +``` diff --git a/btn/SPECIFICATION.md b/btn/SPECIFICATION.md index 4a26ed4..b1bcf3b 100644 --- a/btn/SPECIFICATION.md +++ b/btn/SPECIFICATION.md @@ -8,6 +8,7 @@ The purpose of this specification is to describe overall components which a conf - 24.06.13 :: Minor fix. - 24.06.14 :: Minor fix. - 24.06.18 :: Minor fix. +- 26.05.20 :: Added N = 0 behavior (no line feed insertion). ## Use Case @@ -26,11 +27,12 @@ A conforming implementation must produce a string, hereafter referred to as **th - Any number of multibyte characters can be included in. - As such, a conforming implementation should handle multibyte characters in a proper manner. -- The string must have a line feed character `\n` after every `N` numbers of valid multibyte characters. -- A line feed character must be placed at the end of the result string, unless one have been already put (because of the above condition). +- If `N` is greater than 0, the string must have a line feed character `\n` after every `N` numbers of valid multibyte characters. +- If `N` is greater than 0, a line feed character must be placed at the end of the result string, unless one have been already put (because of the above condition). +- If `N` is 0, no line feed character must be inserted. > Definition. -> `N` is of type `int` and has a value between 1 ~ 32 (inclusive). +> `N` is of type `int` and has a value between 0 ~ 32 (inclusive). ### Example: the result @@ -73,10 +75,10 @@ The implementation must follow the error handling behavior described in the Erro - The number of command line arguments is not equal to 1. - Failure to get the command line argument. -- `N` not in the range of [1, 32]. +- `N` not in the range of [0, 32]. ## Program Name The program name is determined by whether the language in which a conforming implementation is written is of compiled or of interpreted. -If a compiled langauge, then the conforming implementation must have its built executable file have the name `btn`. Otherwise, the conforming implementation must have its source code file have the name `btn` (including a corresponding extension, if needed). \ No newline at end of file +If a compiled langauge, then the conforming implementation must have its built executable file have the name `btn`. Otherwise, the conforming implementation must have its source code file have the name `btn` (including a corresponding extension, if needed). diff --git a/btn/c/btn.c b/btn/c/btn.c index 401a850..31a9fc1 100644 --- a/btn/c/btn.c +++ b/btn/c/btn.c @@ -7,7 +7,8 @@ * Usage: ./btn N < TEXT_FILE_NAME * where * N = the number of characters before a new-line character; - * its range: 1 ~ 32, + * if N = 0, no new-line character is inserted, + * its range: 0 ~ 32, * TEXT_FILE_NAME = the file name to be input-redirected. ***/ @@ -16,7 +17,7 @@ #include #include -#define MIN_CHAR_COUNT 1 +#define MIN_CHAR_COUNT 0 #define MAX_CHAR_COUNT 32 void raise_err(char *err_msg); @@ -64,13 +65,14 @@ int main(int argc, char *argv[]) { if (left_multibyte_count == 0) { printed_char_count++; - if (printed_char_count == num_char_until_newline) { + if (num_char_until_newline > 0 && + printed_char_count == num_char_until_newline) { putchar('\n'); printed_char_count = 0; } } } - if (printed_char_count != 0) + if (num_char_until_newline > 0 && printed_char_count != 0) putchar('\n'); return 0; @@ -104,4 +106,4 @@ int inspect_char(int ch) { } while (--push_amount); return multibyte_len - 1; -} \ No newline at end of file +} diff --git a/btn/haskell/btn.hs b/btn/haskell/btn.hs index d868d98..12a8e77 100644 --- a/btn/haskell/btn.hs +++ b/btn/haskell/btn.hs @@ -12,4 +12,7 @@ main = do (rawCharCountUntilNewline:_) <- getArgs let charCountUntilNewline = read rawCharCountUntilNewline :: Int text <- getContents - putStrLn $ intercalate "\n" . chunks charCountUntilNewline . filter (not . isSpace) $ text + let filteredText = filter (not . isSpace) text + if charCountUntilNewline == 0 + then putStr filteredText + else putStrLn $ intercalate "\n" $ chunks charCountUntilNewline filteredText diff --git a/btn/python/btn.py b/btn/python/btn.py index bf66982..8ac698f 100644 --- a/btn/python/btn.py +++ b/btn/python/btn.py @@ -7,6 +7,10 @@ def main(n): raise_err('nsy: EOF detected.') text = text.translate(str.maketrans('', '', string.whitespace)) + if n == 0: + print(text, end='') + return + for i in range(0, len(text), n): print(text[i : i + n]) @@ -22,7 +26,7 @@ def raise_err(*args, **kwargs): sep='\n') n = int(sys.argv[1]) -if n < 1 or n > 32: +if n < 0 or n > 32: raise_err('nsy: argv[1] out of range.') main(n) diff --git a/test/.test/btn.test b/test/.test/btn.test index c55e40c..b90cf87 100644 --- a/test/.test/btn.test +++ b/test/.test/btn.test @@ -12,4 +12,12 @@ TEST "10", "I hate programming in C so much.\nI'm getting headache.", { "Ihateprogr\namminginCs\nomuch.I'mg\nettinghead\nache.\n" } -) \ No newline at end of file +) + +TEST +( + "no-newline", + "0", + "I hate programming in C so much.\nI'm getting headache.", + { "IhateprogramminginCsomuch.I'mgettingheadache." } +)