Final: port a text pipeline
Lesson 47 of 48 in Coddy's PHP for C Developers course.
Challenge
EasyInput is a list of strings. Port the source reference program supplied in this task: abbreviations removes empty strings, takes the first ASCII character of each remaining string, uppercases it and joins with dots. Preserve order; return none if no nonempty strings. All input is ASCII.
The supplied main script reads one JSON value from standard input, checks the named public APIs, and prints the result. Implement the module declarations without printing; do not replace the launcher or read input from a module. Input satisfies the stated types and bounds.
Edit Functions.php. main.php is the locked entry file. Dependencies use require_once __DIR__ with a relative filename.
C reference program
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
struct Text { const unsigned char *bytes; size_t length; };
struct Result { unsigned char *bytes; size_t length; };
/* Lengths preserve all ASCII bytes, including an embedded zero byte. */
static int abbreviations(const struct Text *words, size_t count, struct Result *out) {
if (count > (SIZE_MAX - 5) / 2) return 0;
unsigned char *result = malloc(count * 2 + 5);
if (result == NULL) return 0;
size_t length = 0;
for (size_t i = 0; i < count; ++i) {
if (words[i].length == 0) continue;
if (length != 0) result[length++] = '.';
unsigned char first = words[i].bytes[0];
if (first >= 'a' && first <= 'z') first = (unsigned char)(first - 'a' + 'A');
result[length++] = first;
}
if (length == 0) { memcpy(result, "none", 4); length = 4; }
*out = (struct Result){result, length};
return 1;
}
int main(void) {
const struct Text words[] = {
{(const unsigned char *)"red", 3}, {(const unsigned char *)"", 0},
{(const unsigned char *)"green", 5}, {(const unsigned char *)"blue", 4}
};
struct Result result;
if (!abbreviations(words, 4, &result)) return 1;
fwrite(result.bytes, 1, result.length, stdout);
putchar('\n');
free(result.bytes);
return ferror(stdout) ? 1 : 0;
}
Try it yourself
<?php
declare(strict_types=1);
require_once __DIR__."/Functions.php";
function ensure(bool $ok,string $message):void{if(!$ok)throw new RuntimeException("API: ".$message);}
$input=json_decode(trim(stream_get_contents(STDIN)),true,512,JSON_THROW_ON_ERROR);
ensure(abbreviations(["a","","b"])==="A.B" && abbreviations([])==="none","port pipeline");
echo abbreviations($input),"\n";
All lessons in PHP for C Developers
1Values and output
Modules and first runFormatting returned textAssignments and variablesDivision and conversionStrings and byte lengthsExplicit Boolean rulesRecap: receipt text4Functions and modules
Functions and return typesDefaults and named callsVariadic argumentsCallbacks and capturesrequire_once and scopeRecap: integer cents2Control flow
Branches and precedenceStrict match expressionsLoops and early exitsKeys and iterationRecap: shipping rules5Errors and boundaries
Exceptions and stateStrict input validationNull is not falsyRecap: validate a batchPractice on your own: Online PHP compiler