[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
/DBT/ - DAILY BIRDMIN THREAD
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

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 replies: 14
Thread images: 1

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

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.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.