[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

/DBT/ - DAILY BIRDMIN THREAD

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 14
Thread images: 1

QUICK, WRITE A SCRIPT IN YOUR FAVOURITE LANGUAGE THAT TOGGLES UPPER/LOWERCASE LETTERS OF A GIVEN FILE AND THE FILENAME ITSELF.

OR THE BIRD IS GOING TO STAB YOU
>>
>>55614622
What encodings does it have to support tho?
>>
>>55614622
UTF-8
>>
>>55614632
utf-8 obv.
>>
>>55614622
exception handling is left as an exercise to the reader
import sys

for arg in sys.argv[1:]:
with open(arg) as fp:
contents = fp.read()
with open(arg.upper(), "w") as out:
out.write(contents.upper())
>>
>>55614856
oops, didn't read the toggle bit
>>
first working solution
#!/bin/bash
path="$1"
dir="$(readlink -f "$(dirname "$path")")"
file="$(basename "$path")"
newpath="${dir}/${file~~}"
contents=$(cat $path)
echo "${contents~~}" > "$newpath"


Example:
cat > Test.TXT
Hello
Wörld

./toggle.bash Test.TXT

cat tEST.txt
hELLO
wÖRLD
>>
>>55614622
#!/usr/bin/env node
"use strict";

const fs = require('fs')

let toggleCase = c => c === c.toLowerCase() ? c.toUpperCase() : c.toLowerCase();

for(let i = 2; i < process.argv.length; i++) {
let fname = process.argv[i];
let buf = fs.readFileSync(fname);
let out = "";
for(let j = 0; j < buf.length; j++) {
out += toggleCase(String.fromCodePoint(buf[j]));
}
fs.writeFileSync(fname, out);
let Fname = "";
for(let j = 0; j < fname.length; j++) {
Fname += toggleCase(fname[j]);
}
fs.rename(fname, Fname);
}
>>
>>55615048
Shorter:
#!/bin/bash
path="$1"
file="$(basename "$path")"
newpath="$(dirname "$path")/${file~~}"
contents="$(<$path)"
echo "${contents~~}" > "$newpath"
# i like ponies
>>
>>55614622
use std::fs::File;
use std::io::{Read, Write};

fn toggled_string(s: &str) -> String {
s.chars()
.map(|c| match c {
'a'...'z' => c.to_uppercase().next().unwrap(),
'A'...'Z' => c.to_lowercase().next().unwrap(),
_ => c
})
.collect()
}

fn toggle_file(filename: &str) {
let mut contents = String::new();

let mut file = File::open(filename).unwrap();
file.read_to_string(&mut contents).unwrap();

let mut file = File::create(&toggled_string(filename)).unwrap();
file.write_all(toggled_string(&contents).as_bytes()).unwrap();
}

fn main() {
let filename = std::env::args()
.nth(1)
.expect("Must provide filename!");

toggle_file(&filename);
}
>>
>>55615141
Only toggles ASCII letters, not Unicode
>>
>>55614622
fs = require('fs');
path = require('path');
file = "/Users/daniel/jej";
filename = path.posix.basename(file);
fs.readFile(file, 'utf8', (error, data) => {
for (var i = 0; i < data.length; i++) {
if(data.charAt(i) == data.charAt(i).toUpperCase}{
data.charAt(i).toUpperCase
} else{
data.charAt(i).toLowerCase
}
}
});
for (var i = 0; i < filename.length; i++) {
if(filename.charAt(i) == data.charAt(i).toUpperCase}{
filename.charAt(i).toUpperCase
} else{
filename.charAt(i).toLowerCase
}
}
note: may not work
>>
Please don't make this into a general.

Trust me, it'll get stale if you do.
>>
Emacs lisp:
(defun toggle-case-file (file)
"Create a file with a name that is toggled case and toggle case in that file"
(interactive (list (read-file-name "File: ")))
(let ((filename (file-name-nondirectory file))
(edit-buffer nil))
(find-file file)
(setq edit-buffer (current-buffer))
(toggle-case-buffer)
(switch-to-buffer "*temp-filename-buffer*")
(insert filename)
(toggle-case-buffer)
(setq filename (buffer-string))
(kill-buffer)
(switch-to-buffer edit-buffer)
(write-file filename)))

(defun toggle-case-buffer ()
"Toggles case on the contents of the buffer."
(goto-char (point-min))
(while (<= (point) (1- (point-max)))
(if (upcasep (following-char))
(insert (downcase (following-char)))
(insert (upcase (following-char))))
(delete-char 1)))

(defun upcasep (char)
"Returns t if char is upcase."
(not (= char (downcase char))))

This code is ugly, but it works. I should have written a function that works on a string, and not a buffer, and I wouldn't need a temporary buffer.
Thread posts: 14
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.