Skip to main content

Timeline for answer to Unix WC Implementation by chux

Current License: CC BY-SA 4.0

Post Revisions

15 events
when toggle format what by license comment
34 mins ago comment added Davislor @chux Good point! I should probably correct myself that the closest C++ Standard Library type is std::streamoff, not std::streampos (which corresponds to mbstate_t). This conversation has more than enough information for anyone reading it to pick one of the options.
2 hours ago comment added chux @Davislor, had the post been tagged POSIX, I'd go for POSIX like solutions. Since it was not, I aim my feedback to be more general C++ than to POISX standards.
4 hours ago comment added Davislor @chux To be language-lawyery, std::uintmax_t is not required in C++23 to hold any type wider than unsigned long long, and it is theoretically possible for an implementation to define std::fpos_t and std::streampos as wider than that. You’re correct that the latter two are not required to be integral types (so it could be an extension like __int128 while uintmax_t is 64 bits wide), and even if they are, their signedness is implementation-defined. But off_t is specified by POSIX to be a signed integer type.
4 hours ago comment added chux @Davislor Hmm, I thought streampos was not certainly an integer and ++ may be work. IAC any 64+ bits integer type is good, at least for the foreseeable future.
4 hours ago comment added Davislor @chux std::uintmax_t is what std::filesystem uses for file sizes, and is guaranteed to be at unsigned and at least 64 bits wide (although not actually to be as wide as any built-in type, in C++23).
5 hours ago comment added chux @Davislor std::fpos_t works well for offsets, etc., yet it is not certainly an integer type. std::uintmax_t and even std::int_least64_t certainly are practical alternatives - until maybe we are all in the old-coders home.
5 hours ago comment added Davislor @chux off_t is supported by Windows, Linux, MacOS and every other mainstream OS, but std::streampos is the C++ standard-library type guaranteed to hold any file size.
5 hours ago comment added chux @Davislor "The -portable type to use for a file length is off_t." --> off_t is not a standard C++ type. It s a POSIX one. So I guess it depends on how portable is the goal, even if WC originated in *nix.
6 hours ago comment added Davislor Or if you want to use a type guaranteed to be in the Standard Library, std::fpos_t or std::streampos should also work for any file size the OS supports. (This might not be true for some old systems that did Large Filename Support in a weird non-standard way.) Or std::int_least64_t is guaranteed to work, for the conceivable future.
6 hours ago comment added Davislor The -portable type to use for a file length is off_t. This will be signed 64-bit on all modern systems, Some might need compiler flags to enable large-file support.
7 hours ago comment added Loki Astari Changed the type for counting to std::uintmax_t this matches what is returned by std::filesystem::file_size()
7 hours ago comment added Loki Astari I assumed wc was smart about line counting. But I just checked its not. So I will revert the code to performing like standard wc. echo -n "A" | wc -l => 0
9 hours ago history edited chux CC BY-SA 4.0
added 684 characters in body
9 hours ago history edited chux CC BY-SA 4.0
added 684 characters in body
9 hours ago history answered chux CC BY-SA 4.0