Use "String" consistently for "char[]" (#694)

Use "String" consistently for "char[]". Fix win32 return value.
This commit is contained in:
Christoffer Lerno
2023-01-07 22:50:33 +01:00
committed by GitHub
parent 5b2b4e900f
commit 43dc2d650c
62 changed files with 273 additions and 246 deletions

View File

@@ -42,7 +42,7 @@ const char PAD = '=';
const char FIRST = '+';
const char LAST = 'z';
fn void encode(char[] in, char *out)
fn void encode(String in, char *out)
{
int j = 0;
char c = LUT_ENC[1];
@@ -76,7 +76,7 @@ fn void encode(char[] in, char *out)
}
fn int! decode(char[] in, char* out, int* invalid_char_index = null)
fn int! decode(String in, char* out, int* invalid_char_index = null)
{
int j = 0;

View File

@@ -5,16 +5,16 @@ fn void main()
{
for (int i = 0; i < 20; i++)
{
String s = bin(i);
VarString s = bin(i);
defer s.destroy();
io::printf("%s\n", s);
}
}
fn String bin(int x)
fn VarString bin(int x)
{
int bits = 1 + (int)(x == 0 ? 0 : math::log10((double)(x)) / math::log10(2));
String str;
VarString str;
str.append_repeat('0', bits);
for (int i = 0; i < bits; i++)
{

View File

@@ -10,13 +10,13 @@ fault InterpretError
INTEPRET_FAILED
}
fn void! print_error(usz pos, char[] err)
fn void! print_error(usz pos, String err)
{
io::printfln("Error at %s: %s", pos, err);
return InterpretError.INTEPRET_FAILED!;
}
fn void! brainf(char[] program)
fn void! brainf(String program)
{
usz sp = 0;
usz mem = 0;
@@ -76,7 +76,7 @@ fn void! brainf(char[] program)
}
fn void! main()
{
char[] program = `
String program = `
++++[>+++++<-]>[<+++++>-]+<+[
>[>+>+<<-]++>>[<<+>>-]>>>[-]++>[-]+
>>>+[[-]++++++>>>]<<<[[<++++++++<++>>-]+<.<[>----<-]<]

View File

@@ -3,11 +3,11 @@ import libc;
import std::io;
struct Doc { Head *head; }
struct Head { String* title; }
struct Head { VarString* title; }
struct Summary
{
String* title;
VarString* title;
bool ok;
}
@@ -21,11 +21,11 @@ private struct StringData
fn void Summary.print(Summary *s, File out)
{
char[] title = s.title ? s.title.str() : "missing";
String title = s.title ? s.title.str() : "missing";
out.printf("Summary({ .title = %s, .ok = %s})", title, s.ok);
}
fn bool contains(char[] haystack, char[] needle)
fn bool contains(String haystack, String needle)
{
usz len = haystack.len;
usz needle_len = needle.len;
@@ -54,13 +54,13 @@ fault ReadError
BAD_READ,
}
fn Doc! readDoc(char[] url)
fn Doc! readDoc(String url)
{
if (contains(url, "fail")) return ReadError.BAD_READ!;
if (contains(url, "head-missing")) return { .head = null };
if (contains(url, "title-missing")) return { @dupe(Head { .title = null }) };
if (contains(url, "title-empty")) return { @dupe(Head { .title = @dupe((String)null) }) };
String str;
if (contains(url, "title-empty")) return { @dupe(Head { .title = @dupe((VarString)null) }) };
VarString str;
str.printf("Title of %s", url);
return { @dupe(Head { .title = @dupe(str) }) };
}
@@ -73,7 +73,7 @@ fn Summary buildSummary(Doc doc)
};
}
fn Summary readAndBuildSummary(char[] url)
fn Summary readAndBuildSummary(String url)
{
return buildSummary(readDoc(url)) ?? Summary { .title = null, .ok = false };
/*
@@ -97,18 +97,18 @@ fault TitleResult
fn bool! isTitleNonEmpty(Doc doc)
{
if (!doc.head) return TitleResult.TITLE_MISSING!;
String* head = doc.head.title;
VarString* head = doc.head.title;
if (!head) return TitleResult.TITLE_MISSING!;
return head.len() > 0;
}
fn bool! readWhetherTitleNonEmpty(char[] url)
fn bool! readWhetherTitleNonEmpty(String url)
{
return isTitleNonEmpty(readDoc(url));
}
fn char[] bool_to_string(bool b)
fn String bool_to_string(bool b)
{
return b ? "true" : "false";
}
@@ -116,10 +116,10 @@ fn char[] bool_to_string(bool b)
fn void main()
{
const char[][] URLS = { "good", "title-empty", "title-missing", "head-missing", "fail" };
const String[] URLS = { "good", "title-empty", "title-missing", "head-missing", "fail" };
DynamicArenaAllocator dynamic_arena;
dynamic_arena.init(1024);
foreach (char[] url : URLS)
foreach (String url : URLS)
{
mem::@scoped(&dynamic_arena)
{
@@ -128,7 +128,7 @@ fn void main()
io::printf(" Summary: ");
summary.print(io::stdout());
io::println("");
char[] title_sure = summary.title ? summary.title.str() : "";
String title_sure = summary.title ? summary.title.str() : "";
io::printf(" Title: %s\n", title_sure);
bool! has_title = readWhetherTitleNonEmpty(url);
// This looks a bit less than elegant, but as you see it's mostly due to having to

View File

@@ -1,7 +1,7 @@
module foo;
import std::io;
tlocal char[] context_user = "safe";
tlocal String context_user = "safe";
macro long reallyPerform(task)
{
@@ -14,7 +14,7 @@ macro long perform(task)
return reallyPerform(task);
}
macro @with_mode(char[] user, #action, ...)
macro @with_mode(String user, #action, ...)
{
@scope(context_user)
{
@@ -27,6 +27,6 @@ fn void main()
{
long val1 = perform("something");
long val2 = @with_mode("faster", perform, "reliable");
long val3 = perform(char[][] {"something"});
long val3 = perform(String[] {"something"});
io::printfln("%d %d %d", val1, val2, val3);
}

View File

@@ -23,14 +23,14 @@ int err_count = 0;
fn int! askGuess(int high)
{
libc::printf("Guess a number between 1 and %d: ", high);
char[] text = readLine()?;
String text = readLine()?;
char* end = null;
int value = (int)libc::strtol(text.ptr, &end, 10);
if (end && end[0] >= ' ') return InputResult.NOT_AN_INT!;
return value;
}
fn char[]! readLine()
fn String! readLine()
{
char* chars = tmalloc(1024)?;
isz loaded = getline(&chars, &&(usz)1023, libc::stdin());
@@ -67,7 +67,7 @@ fn void! Game.play(Game *game)
fn void Game.report(Game *game, int guess)
{
char[] desc = {|
String desc = {|
if (guess < game.answer) return "too low";
if (guess > game.answer) return "too high";
return "the answer";

View File

@@ -9,7 +9,7 @@ fn void main()
And a nested comment.
*/
*/
char[] text = `
String text = `
function hello() {
console.log("name`"\t"`age");
}

View File

@@ -15,7 +15,7 @@ fn float fasta_rand(float max_val)
return max_val * seed / IM;
}
private char[] alu =
private String alu =
"GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG"
"GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA"
"CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT"
@@ -25,7 +25,7 @@ private char[] alu =
"AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA";
char[] iub = "acgtBDHKMNRSVWY";
String iub = "acgtBDHKMNRSVWY";
double[] iub_p = {
0.27,
0.12,
@@ -43,7 +43,7 @@ double[] iub_p = {
0.02,
0.02 };
char[] homosapiens = "acgt";
String homosapiens = "acgt";
double[] homosapiens_p = {
0.3029549426680,
0.1979883004921,
@@ -54,7 +54,7 @@ double[] homosapiens_p = {
const LINELEN = 60;
// slowest character-at-a-time output
fn void repeat_fasta(char[] seq, int n)
fn void repeat_fasta(String seq, int n)
{
usz len = seq.len;
int i = void;
@@ -66,7 +66,7 @@ fn void repeat_fasta(char[] seq, int n)
if (i % LINELEN != 0) io::putchar('\n');
}
fn void random_fasta(char[] symb, double[] probability, int n)
fn void random_fasta(String symb, double[] probability, int n)
{
assert(symb.len == probability.len);
int len = probability.len;

View File

@@ -7,7 +7,7 @@ import libc;
fn void main()
{
char[] y = "Hello World!";
String y = "Hello World!";
libc::printf("Adler32 of %s is %x, expected 1c49043e\n", (char*)(y), adler32(y));
libc::printf("CRC32B of %s is %x, expected 1c291ca3\n", (char*)(y), crc32(y));
libc::printf("CRC64 of %s is %llx, expected fad9a77c67077205\n", (char*)(y), crc64(y));

View File

@@ -2,7 +2,7 @@ import std::io;
fn void main()
{
char[][] greetings = {
String[] greetings = {
"Hello, world!",
"¡Hola Mundo!",
"Γειά σου Κόσμε!",

View File

@@ -2,7 +2,7 @@ module levenshtein;
import std::math;
// This levenshtein exercises C3 subarrays.
fn int levenshtein(char[] s, char[] t)
fn int levenshtein(String s, String t)
{
// if either string is empty, difference is inserting all chars
// from the other

View File

@@ -142,7 +142,7 @@ fn void scale_bodies(Planet[] bodies, double scale)
}
fn void main(char[][] args)
fn void main(String[] args)
{
int n = args.len < 2 ? 50000000 : str::to_int(args[1])!!;

View File

@@ -4,10 +4,10 @@ import regex, stdio;
fn void main()
{
println("Enter a story template, terminated by an empty line:");
String story = "";
VarString story = "";
while (1)
{
String line = stdin.readln().strip() else break;
VarString line = stdin.readln().strip() else break;
story = story.append(line);
story = story.append("\n");
}
@@ -19,7 +19,7 @@ fn void main()
foreach (RegexMatch* match : r.match(story))
{
String s = match.string;
VarString s = match.string;
printf("Enter a value for '%s': ", s[1..^2]);
string word = stdin.readln().strip() else return;
story = story.replace(s, word);

View File

@@ -7,7 +7,7 @@ import stdlib;
const uint MaxText = 1024;
enum TokenKind : char (String name)
enum TokenKind : char (VarString name)
{
WORD("word"),
TEXT("text"),

View File

@@ -10,15 +10,15 @@ fault TokenResult
// While we could have written this with libc
// the C way, let's showcase some features added to C3.
fn void main(char[][] args)
fn void main(String[] args)
{
// Grab a string from stdin
String s = io::stdin().getline();
VarString s = io::stdin().getline();
// Delete it at scope end [defer]
defer s.destroy();
// Grab the string as a slice.
char[] numbers = s.str();
String numbers = s.str();
// Track our current value
int val = 0;
@@ -26,7 +26,7 @@ fn void main(char[][] args)
// Is the current state an add?
bool add = true;
while (try char[] token = read_next(&numbers))
while (try String token = read_next(&numbers))
{
// We're assuming well formed input here
// so just use atoi.
@@ -36,7 +36,7 @@ fn void main(char[][] args)
val = add ? val + i : val - i;
// Read an optional token.
char[]! op = read_next(&numbers);
String! op = read_next(&numbers);
// If it's an error, then we're done.
if (catch op) break;
@@ -56,7 +56,7 @@ fn void main(char[][] args)
io::printfln("%d", val);
}
fn char[]! read_next(char[]* remaining)
fn String! read_next(String* remaining)
{
while (remaining.len > 0 && (*remaining)[0] == ' ')
{

View File

@@ -42,7 +42,7 @@ fn void eval_AtA_times_u(double[] u, double[] atau, double[] x)
eval_At_times_u(temparr, atau, x);
}
fn void main(char[][] args)
fn void main(String[] args)
{
int n = args.len == 2 ? str::to_int(args[1])!! : 2000;
temparr = array::alloc(double, n);

View File

@@ -12,7 +12,7 @@ fn void print_pages()
mem::temp_allocator().print_pages(io::stdout());
}
fn void setstring(char* dst, char[] str)
fn void setstring(char* dst, String str)
{
foreach (char c : str)
{

View File

@@ -143,7 +143,7 @@ $else
generic boofer2(i, g, eok)
{
case int, char[], type($eoo):
case int, String, type($eoo):
return "Helo";
default:
return 1000;

View File

@@ -2,7 +2,7 @@ module tmem;
import std::mem;
import std::io;
struct String
struct VarString
{
Allocator allocator;
usz len;
@@ -10,7 +10,7 @@ struct String
char* ptr;
}
fn void String.init(String *s, char[] c)
fn void VarString.init(VarString *s, String c)
{
s.capacity = c.len + 16;
s.ptr = malloc(s.capacity);
@@ -18,7 +18,7 @@ fn void String.init(String *s, char[] c)
mem::copy(s.ptr, (char*)(c), c.len);
}
fn char* String.zstr(String *s)
fn char* VarString.zstr(VarString *s)
{
char* c = malloc(s.len + 1);
mem::copy(c, s.ptr, s.len);
@@ -26,7 +26,7 @@ fn char* String.zstr(String *s)
return c;
}
fn void String.appendc(String *s, char c)
fn void VarString.appendc(VarString *s, char c)
{
if (s.capacity == s.len)
{
@@ -38,7 +38,7 @@ fn void String.appendc(String *s, char c)
s.ptr[s.len++] = c;
}
fn void String.append(String *s, char[] other_string)
fn void VarString.append(VarString *s, String other_string)
{
if (s.capacity < s.len + other_string.len)
{
@@ -55,7 +55,7 @@ fn void String.append(String *s, char[] other_string)
s.len += other_string.len;
}
fn void String.concat(String *s, String* other_string)
fn void VarString.concat(VarString *s, VarString* other_string)
{
if (s.capacity < s.len + other_string.len)
{
@@ -74,12 +74,12 @@ fn void String.concat(String *s, String* other_string)
fn void main()
{
String s;
VarString s;
s.init("Hello");
s.appendc(' ');
s.appendc('W');
s.append("orld!");
String w;
VarString w;
w.init("Yo man!");
s.concat(&w);
libc::printf("Message was: %s\n", s.zstr());

View File

@@ -27,7 +27,6 @@
"hello_world_win32": {
"type": "executable",
"c-sources-override": [
"./csource/**"
]
},
"hello_world_lib": {