Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions btn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
```

Expand All @@ -31,4 +32,4 @@ python btn.py N < TEXT_FILE_NAME

```
ghc buntang.hs
```
```
12 changes: 7 additions & 5 deletions btn/SPECIFICATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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).
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).
12 changes: 7 additions & 5 deletions btn/c/btn.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
***/

Expand All @@ -16,7 +17,7 @@
#include <stdlib.h>
#include <errno.h>

#define MIN_CHAR_COUNT 1
#define MIN_CHAR_COUNT 0
#define MAX_CHAR_COUNT 32

void raise_err(char *err_msg);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -104,4 +106,4 @@ int inspect_char(int ch) {
} while (--push_amount);

return multibyte_len - 1;
}
}
5 changes: 4 additions & 1 deletion btn/haskell/btn.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion btn/python/btn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand All @@ -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)
10 changes: 9 additions & 1 deletion test/.test/btn.test
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
)
)

TEST
(
"no-newline",
"0",
"I hate programming in C so much.\nI'm getting headache.",
{ "IhateprogramminginCsomuch.I'mgettingheadache." }
)
Loading