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 readerimport 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.TXTcat 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
>>55614622use 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
>>55614622fs = 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.