├── .gitignore ├── Doxyfile ├── LICENSE ├── README.md ├── docs ├── bc_s.png ├── bc_sd.png ├── clipboard.js ├── closed.png ├── doc.svg ├── docd.svg ├── doxygen.css ├── doxygen.svg ├── doxygen_crawl.html ├── dynsections.js ├── files.html ├── folderclosed.svg ├── folderclosedd.svg ├── folderopen.svg ├── folderopend.svg ├── globals.html ├── globals_defs.html ├── globals_func.html ├── index.html ├── jquery.js ├── menu.js ├── menudata.js ├── minus.svg ├── minusd.svg ├── nav_f.png ├── nav_fd.png ├── nav_g.png ├── nav_h.png ├── nav_hd.png ├── navtree.css ├── open.png ├── plus.svg ├── plusd.svg ├── resize.js ├── splitbar.png ├── splitbard.png ├── sync_off.png ├── sync_on.png ├── tab_a.png ├── tab_ad.png ├── tab_b.png ├── tab_bd.png ├── tab_h.png ├── tab_hd.png ├── tab_s.png ├── tab_sd.png ├── tabs.css ├── uuidv7_8h.html └── uuidv7_8h_source.html ├── test ├── Makefile ├── impl_new_unix.c ├── test_core.c └── test_new.c └── uuidv7.h /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # uuidv7.h - Single-file C/C++ UUIDv7 Library 2 | 3 | Examples: 4 | 5 | ```c 6 | #include "uuidv7.h" 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int main(void) { 13 | // use high-level APIs that require concrete `uuidv7_new()` implementation 14 | char text[37]; 15 | for (int i = 0; i < 8; i++) { 16 | uuidv7_new_string(text); 17 | puts(text); 18 | } 19 | 20 | // generate a UUIDv7 with the current Unix time using the low-level APIs 21 | struct timespec tp; 22 | clock_gettime(CLOCK_REALTIME, &tp); 23 | uint64_t unix_ts_ms = (uint64_t)tp.tv_sec * 1000 + tp.tv_nsec / 1000000; 24 | 25 | uint8_t rand_bytes[10]; 26 | getentropy(rand_bytes, 10); 27 | 28 | uint8_t uuid[16]; 29 | uuidv7_generate(uuid, unix_ts_ms, rand_bytes, NULL); 30 | uuidv7_to_string(uuid, text); 31 | puts(text); 32 | 33 | // generate another while guaranteeing ascending order of UUIDs 34 | clock_gettime(CLOCK_REALTIME, &tp); 35 | unix_ts_ms = (uint64_t)tp.tv_sec * 1000 + tp.tv_nsec / 1000000; 36 | getentropy(rand_bytes, 10); 37 | 38 | int status = uuidv7_generate(uuid, unix_ts_ms, rand_bytes, uuid); 39 | if (status == UUIDV7_STATUS_CLOCK_ROLLBACK) 40 | return 1; // error: clock moved backward by more than 10 seconds 41 | uuidv7_to_string(uuid, text); 42 | puts(text); 43 | 44 | return 0; // success 45 | } 46 | 47 | /** 48 | * Generates a new UUIDv7 with the current Unix time. 49 | * 50 | * `uuidv7.h` provides the primitive `uuidv7_generate()` function only. Users 51 | * have to integrate a real-time clock, cryptographically strong random number 52 | * generator, and shared state storage available in the target platform. 53 | * 54 | * @warning This example uses static variables and is NOT thread-safe. 55 | */ 56 | int uuidv7_new(uint8_t *uuid_out) { 57 | static uint8_t uuid_prev[16] = {0}; 58 | static uint8_t rand_bytes[10] = {0}; 59 | static int n_rand_consumed = 10; 60 | 61 | struct timespec tp; 62 | clock_gettime(CLOCK_REALTIME, &tp); 63 | uint64_t unix_ts_ms = (uint64_t)tp.tv_sec * 1000 + tp.tv_nsec / 1000000; 64 | 65 | getentropy(rand_bytes, n_rand_consumed); 66 | int8_t status = uuidv7_generate(uuid_prev, unix_ts_ms, rand_bytes, uuid_prev); 67 | n_rand_consumed = uuidv7_status_n_rand_consumed(status); 68 | 69 | for (int i = 0; i < 16; i++) 70 | uuid_out[i] = uuid_prev[i]; 71 | return status; 72 | } 73 | ``` 74 | 75 | See [RFC 9562](https://www.rfc-editor.org/rfc/rfc9562). 76 | 77 | ## Primary function 78 | 79 | ```c 80 | static inline int8_t uuidv7_generate(uint8_t *uuid_out, uint64_t unix_ts_ms, 81 | const uint8_t *rand_bytes, 82 | const uint8_t *uuid_prev); 83 | ``` 84 | 85 | Generates a new UUIDv7 from the given Unix time, random bytes, and previous UUID. 86 | 87 | - **Parameters:** 88 | - `uuid_out`: 16-byte byte array where the generated UUID is stored. 89 | - `unix_ts_ms`: Current Unix time in milliseconds. 90 | - `rand_bytes`: At least 10-byte byte array filled with random bytes. This 91 | function consumes the leading 4 bytes or the whole 10 bytes per call 92 | depending on the conditions. `uuidv7_status_n_rand_consumed()` maps the 93 | return value of this function to the number of random bytes consumed. 94 | - `uuid_prev`: 16-byte byte array representing the immediately preceding UUID, 95 | from which the previous timestamp and counter are extracted. This may be 96 | NULL if the caller does not care the ascending order of UUIDs within the 97 | same timestamp. This may point to the same location as `uuid_out`; this 98 | function reads the value before writing. 99 | - **Returns:** 100 | - One of the `UUIDV7_STATUS_*` codes that describe the characteristics of 101 | generated UUIDs. Callers can usually ignore the status unless they need to 102 | guarantee the monotonic order of UUIDs or fine-tune the generation process. 103 | 104 | See [API reference](https://liosk.github.io/uuidv7-h/uuidv7_8h.html) for the 105 | full list of provided functions. 106 | 107 | ## Field and bit layout 108 | 109 | This implementation produces identifiers with the following bit layout: 110 | 111 | ```text 112 | 0 1 2 3 113 | 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 114 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 115 | | unix_ts_ms | 116 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 117 | | unix_ts_ms | ver | counter | 118 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 119 | |var| counter | 120 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 121 | | rand | 122 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 123 | ``` 124 | 125 | Where: 126 | 127 | - The 48-bit `unix_ts_ms` field is dedicated to the Unix timestamp in 128 | milliseconds. 129 | - The 4-bit `ver` field is set at `0111`. 130 | - The 42-bit `counter` field accommodates a counter that ensures the increasing 131 | order of IDs generated within a millisecond. The counter is incremented by one 132 | for each new ID and is reset to a random number when the `unix_ts_ms` changes. 133 | - The 2-bit `var` field is set at `10`. 134 | - The remaining 32 `rand` bits are filled with a random number. 135 | 136 | The 42-bit `counter` is sufficiently large, so you do not usually need to worry 137 | about overflow, but in an extremely rare circumstance where it overflows, this 138 | library increments the `unix_ts_ms` field to continue instant monotonic 139 | generation. As a result, the generated ID may have a greater `unix_ts_ms` value 140 | than that passed as the argument. (See also [Why so large counter? (42bits)]). 141 | 142 | UUIDv7, by design, relies on the system clock to guarantee the monotonically 143 | increasing order of generated IDs. A generator may not be able to produce a 144 | monotonic sequence if the system clock goes backwards. This library ignores a 145 | rollback of provided `unix_ts_ms` and reuses the one in `uuid_prev` unless the 146 | rollback is considered significant (namely, more than ten seconds). If such a 147 | significant rollback takes place, this library ignores the `uuid_prev` argument 148 | and thus breaks the increasing order of generated IDs. 149 | 150 | [Why so large counter? (42bits)]: https://github.com/LiosK/uuidv7/issues/13#issuecomment-2306922356 151 | 152 | ## License 153 | 154 | Licensed under the Apache License, Version 2.0. 155 | -------------------------------------------------------------------------------- /docs/bc_s.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiosK/uuidv7-h/9ae44fec0eb2843d5230caac484b0388c93b836b/docs/bc_s.png -------------------------------------------------------------------------------- /docs/bc_sd.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiosK/uuidv7-h/9ae44fec0eb2843d5230caac484b0388c93b836b/docs/bc_sd.png -------------------------------------------------------------------------------- /docs/clipboard.js: -------------------------------------------------------------------------------- 1 | /** 2 | 3 | The code below is based on the Doxygen Awesome project, see 4 | https://github.com/jothepro/doxygen-awesome-css 5 | 6 | MIT License 7 | 8 | Copyright (c) 2021 - 2022 jothepro 9 | 10 | Permission is hereby granted, free of charge, to any person obtaining a copy 11 | of this software and associated documentation files (the "Software"), to deal 12 | in the Software without restriction, including without limitation the rights 13 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | copies of the Software, and to permit persons to whom the Software is 15 | furnished to do so, subject to the following conditions: 16 | 17 | The above copyright notice and this permission notice shall be included in all 18 | copies or substantial portions of the Software. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | SOFTWARE. 27 | 28 | */ 29 | 30 | let clipboard_title = "Copy to clipboard" 31 | let clipboard_icon = `` 32 | let clipboard_successIcon = `` 33 | let clipboard_successDuration = 1000 34 | 35 | $(function() { 36 | if(navigator.clipboard) { 37 | const fragments = document.getElementsByClassName("fragment") 38 | for(const fragment of fragments) { 39 | const clipboard_div = document.createElement("div") 40 | clipboard_div.classList.add("clipboard") 41 | clipboard_div.innerHTML = clipboard_icon 42 | clipboard_div.title = clipboard_title 43 | $(clipboard_div).click(function() { 44 | const content = this.parentNode.cloneNode(true) 45 | // filter out line number and folded fragments from file listings 46 | content.querySelectorAll(".lineno, .ttc, .foldclosed").forEach((node) => { node.remove() }) 47 | let text = content.textContent 48 | // remove trailing newlines and trailing spaces from empty lines 49 | text = text.replace(/^\s*\n/gm,'\n').replace(/\n*$/,'') 50 | navigator.clipboard.writeText(text); 51 | this.classList.add("success") 52 | this.innerHTML = clipboard_successIcon 53 | window.setTimeout(() => { // switch back to normal icon after timeout 54 | this.classList.remove("success") 55 | this.innerHTML = clipboard_icon 56 | }, clipboard_successDuration); 57 | }) 58 | fragment.insertBefore(clipboard_div, fragment.firstChild) 59 | } 60 | } 61 | }) 62 | -------------------------------------------------------------------------------- /docs/closed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiosK/uuidv7-h/9ae44fec0eb2843d5230caac484b0388c93b836b/docs/closed.png -------------------------------------------------------------------------------- /docs/doc.svg: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /docs/docd.svg: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /docs/doxygen.svg: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /docs/doxygen_crawl.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Validator / crawler helper 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /docs/dynsections.js: -------------------------------------------------------------------------------- 1 | /* 2 | @licstart The following is the entire license notice for the JavaScript code in this file. 3 | 4 | The MIT License (MIT) 5 | 6 | Copyright (C) 1997-2020 by Dimitri van Heesch 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software 9 | and associated documentation files (the "Software"), to deal in the Software without restriction, 10 | including without limitation the rights to use, copy, modify, merge, publish, distribute, 11 | sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in all copies or 15 | substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 18 | BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 20 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | @licend The above is the entire license notice for the JavaScript code in this file 24 | */ 25 | 26 | function toggleVisibility(linkObj) { 27 | return dynsection.toggleVisibility(linkObj); 28 | } 29 | 30 | let dynsection = { 31 | 32 | // helper function 33 | updateStripes : function() { 34 | $('table.directory tr'). 35 | removeClass('even').filter(':visible:even').addClass('even'); 36 | $('table.directory tr'). 37 | removeClass('odd').filter(':visible:odd').addClass('odd'); 38 | }, 39 | 40 | toggleVisibility : function(linkObj) { 41 | const base = $(linkObj).attr('id'); 42 | const summary = $('#'+base+'-summary'); 43 | const content = $('#'+base+'-content'); 44 | const trigger = $('#'+base+'-trigger'); 45 | const src=$(trigger).attr('src'); 46 | if (content.is(':visible')===true) { 47 | content.hide(); 48 | summary.show(); 49 | $(linkObj).addClass('closed').removeClass('opened'); 50 | $(trigger).attr('src',src.substring(0,src.length-8)+'closed.png'); 51 | } else { 52 | content.show(); 53 | summary.hide(); 54 | $(linkObj).removeClass('closed').addClass('opened'); 55 | $(trigger).attr('src',src.substring(0,src.length-10)+'open.png'); 56 | } 57 | return false; 58 | }, 59 | 60 | toggleLevel : function(level) { 61 | $('table.directory tr').each(function() { 62 | const l = this.id.split('_').length-1; 63 | const i = $('#img'+this.id.substring(3)); 64 | const a = $('#arr'+this.id.substring(3)); 65 | if (l'); 166 | // add vertical lines to other rows 167 | $('span[class=lineno]').not(':eq(0)').append(''); 168 | // add toggle controls to lines with fold divs 169 | $('div[class=foldopen]').each(function() { 170 | // extract specific id to use 171 | const id = $(this).attr('id').replace('foldopen',''); 172 | // extract start and end foldable fragment attributes 173 | const start = $(this).attr('data-start'); 174 | const end = $(this).attr('data-end'); 175 | // replace normal fold span with controls for the first line of a foldable fragment 176 | $(this).find('span[class=fold]:first').replaceWith(''); 179 | // append div for folded (closed) representation 180 | $(this).after(''); 181 | // extract the first line from the "open" section to represent closed content 182 | const line = $(this).children().first().clone(); 183 | // remove any glow that might still be active on the original line 184 | $(line).removeClass('glow'); 185 | if (start) { 186 | // if line already ends with a start marker (e.g. trailing {), remove it 187 | $(line).html($(line).html().replace(new RegExp('\\s*'+start+'\\s*$','g'),'')); 188 | } 189 | // replace minus with plus symbol 190 | $(line).find('span[class=fold]').css('background-image',codefold.plusImg[relPath]); 191 | // append ellipsis 192 | $(line).append(' '+start+''+end); 193 | // insert constructed line into closed div 194 | $('#foldclosed'+id).html(line); 195 | }); 196 | }, 197 | }; 198 | /* @license-end */ 199 | -------------------------------------------------------------------------------- /docs/files.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | uuidv7.h: File List 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 |
20 | 21 | 22 | 23 | 27 | 28 | 29 |
24 |
uuidv7.h 25 |
26 |
30 |
31 | 32 | 33 | 38 | 39 | 40 | 47 | 48 |
49 |
50 | 55 |
56 |
File List
57 |
58 |
59 |
Here is a list of all documented files with brief descriptions:
60 | 61 | 62 |
 uuidv7.hUuidv7.h - Single-file C/C++ UUIDv7 Library
63 |
64 |
65 | 66 | 69 |
70 | 71 | 72 | -------------------------------------------------------------------------------- /docs/folderclosed.svg: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /docs/folderclosedd.svg: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /docs/folderopen.svg: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 9 | 10 | 11 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /docs/folderopend.svg: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /docs/globals.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | uuidv7.h: Globals 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 |
20 | 21 | 22 | 23 | 27 | 28 | 29 |
24 |
uuidv7.h 25 |
26 |
30 |
31 | 32 | 33 | 38 | 39 | 40 | 47 | 48 |
49 |
50 | 55 |
56 |
Here is a list of all documented functions, variables, defines, enums, and typedefs with links to the documentation:
    57 |
  • uuidv7_from_string() : uuidv7.h
  • 58 |
  • uuidv7_generate() : uuidv7.h
  • 59 |
  • uuidv7_new() : uuidv7.h
  • 60 |
  • uuidv7_new_string() : uuidv7.h
  • 61 |
  • UUIDV7_STATUS_CLOCK_ROLLBACK : uuidv7.h
  • 62 |
  • UUIDV7_STATUS_COUNTER_INC : uuidv7.h
  • 63 |
  • UUIDV7_STATUS_ERR_TIMESTAMP : uuidv7.h
  • 64 |
  • UUIDV7_STATUS_ERR_TIMESTAMP_OVERFLOW : uuidv7.h
  • 65 |
  • uuidv7_status_n_rand_consumed() : uuidv7.h
  • 66 |
  • UUIDV7_STATUS_NEW_TIMESTAMP : uuidv7.h
  • 67 |
  • UUIDV7_STATUS_TIMESTAMP_INC : uuidv7.h
  • 68 |
  • UUIDV7_STATUS_UNPRECEDENTED : uuidv7.h
  • 69 |
  • uuidv7_to_string() : uuidv7.h
  • 70 |
71 |
72 | 73 | 76 |
77 | 78 | 79 | -------------------------------------------------------------------------------- /docs/globals_defs.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | uuidv7.h: Globals 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 |
20 | 21 | 22 | 23 | 27 | 28 | 29 |
24 |
uuidv7.h 25 |
26 |
30 |
31 | 32 | 33 | 38 | 39 | 40 | 47 | 48 |
49 |
50 | 55 |
56 |
Here is a list of all documented macros with links to the documentation:
    57 |
  • UUIDV7_STATUS_CLOCK_ROLLBACK : uuidv7.h
  • 58 |
  • UUIDV7_STATUS_COUNTER_INC : uuidv7.h
  • 59 |
  • UUIDV7_STATUS_ERR_TIMESTAMP : uuidv7.h
  • 60 |
  • UUIDV7_STATUS_ERR_TIMESTAMP_OVERFLOW : uuidv7.h
  • 61 |
  • UUIDV7_STATUS_NEW_TIMESTAMP : uuidv7.h
  • 62 |
  • UUIDV7_STATUS_TIMESTAMP_INC : uuidv7.h
  • 63 |
  • UUIDV7_STATUS_UNPRECEDENTED : uuidv7.h
  • 64 |
65 |
66 | 67 | 70 |
71 | 72 | 73 | -------------------------------------------------------------------------------- /docs/globals_func.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | uuidv7.h: Globals 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 |
20 | 21 | 22 | 23 | 27 | 28 | 29 |
24 |
uuidv7.h 25 |
26 |
30 |
31 | 32 | 33 | 38 | 39 | 40 | 47 | 48 |
49 |
50 | 55 |
56 |
Here is a list of all documented functions with links to the documentation:
64 |
65 | 66 | 69 |
70 | 71 | 72 | -------------------------------------------------------------------------------- /docs/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | uuidv7.h: uuidv7.h - Single-file C/C++ UUIDv7 Library 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 |
20 | 21 | 22 | 23 | 27 | 28 | 29 |
24 |
uuidv7.h 25 |
26 |
30 |
31 | 32 | 33 | 38 | 39 | 40 | 47 | 48 |
49 |
50 | 55 |
56 |
uuidv7.h - Single-file C/C++ UUIDv7 Library
57 |
58 |
59 |

60 |

Examples:

61 |
#include "uuidv7.h"
62 |
63 |
#include <stdio.h>
64 |
#include <sys/random.h>
65 |
#include <time.h>
66 |
67 |
int main(void) {
68 |
// use high-level APIs that require concrete `uuidv7_new()` implementation
69 |
char text[37];
70 |
for (int i = 0; i < 8; i++) {
71 | 72 |
puts(text);
73 |
}
74 |
75 |
// generate a UUIDv7 with the current Unix time using the low-level APIs
76 |
struct timespec tp;
77 |
clock_gettime(CLOCK_REALTIME, &tp);
78 |
uint64_t unix_ts_ms = (uint64_t)tp.tv_sec * 1000 + tp.tv_nsec / 1000000;
79 |
80 |
uint8_t rand_bytes[10];
81 |
getentropy(rand_bytes, 10);
82 |
83 |
uint8_t uuid[16];
84 |
uuidv7_generate(uuid, unix_ts_ms, rand_bytes, NULL);
85 |
uuidv7_to_string(uuid, text);
86 |
puts(text);
87 |
88 |
// generate another while guaranteeing ascending order of UUIDs
89 |
clock_gettime(CLOCK_REALTIME, &tp);
90 |
unix_ts_ms = (uint64_t)tp.tv_sec * 1000 + tp.tv_nsec / 1000000;
91 |
getentropy(rand_bytes, 10);
92 |
93 |
int status = uuidv7_generate(uuid, unix_ts_ms, rand_bytes, uuid);
94 | 95 |
return 1; // error: clock moved backward by more than 10 seconds
96 |
uuidv7_to_string(uuid, text);
97 |
puts(text);
98 |
99 |
return 0; // success
100 |
}
101 |
102 |
/**
103 |
* Generates a new UUIDv7 with the current Unix time.
104 |
*
105 |
* `uuidv7.h` provides the primitive `uuidv7_generate()` function only. Users
106 |
* have to integrate a real-time clock, cryptographically strong random number
107 |
* generator, and shared state storage available in the target platform.
108 |
*
109 |
* @warning This example uses static variables and is NOT thread-safe.
110 |
*/
111 |
int uuidv7_new(uint8_t *uuid_out) {
112 |
static uint8_t uuid_prev[16] = {0};
113 |
static uint8_t rand_bytes[10] = {0};
114 |
static int n_rand_consumed = 10;
115 |
116 |
struct timespec tp;
117 |
clock_gettime(CLOCK_REALTIME, &tp);
118 |
uint64_t unix_ts_ms = (uint64_t)tp.tv_sec * 1000 + tp.tv_nsec / 1000000;
119 |
120 |
getentropy(rand_bytes, n_rand_consumed);
121 |
int8_t status = uuidv7_generate(uuid_prev, unix_ts_ms, rand_bytes, uuid_prev);
122 |
n_rand_consumed = uuidv7_status_n_rand_consumed(status);
123 |
124 |
for (int i = 0; i < 16; i++)
125 |
uuid_out[i] = uuid_prev[i];
126 |
return status;
127 |
}
128 |
uuidv7.h - Single-file C/C++ UUIDv7 Library
129 |
static void uuidv7_to_string(const uint8_t *uuid, char *string_out)
Encodes a UUID in the 8-4-4-4-12 hexadecimal string representation.
Definition uuidv7.h:203
130 |
static int8_t uuidv7_generate(uint8_t *uuid_out, uint64_t unix_ts_ms, const uint8_t *rand_bytes, const uint8_t *uuid_prev)
Generates a new UUIDv7 from the given Unix time, random bytes, and previous UUID.
Definition uuidv7.h:112
131 |
#define UUIDV7_STATUS_CLOCK_ROLLBACK
Indicates that the monotonic order of generated UUIDs was broken because the unix_ts_ms passed was le...
Definition uuidv7.h:66
132 |
static int uuidv7_status_n_rand_consumed(int8_t status)
Determines the number of random bytes consumsed by uuidv7_generate() from the UUIDV7_STATUS_* code re...
Definition uuidv7.h:192
133 |
static int uuidv7_new_string(char *string_out)
Generates an 8-4-4-4-12 hexadecimal string representation of new UUIDv7.
Definition uuidv7.h:294
134 |
int uuidv7_new(uint8_t *uuid_out)
Generates a new UUIDv7 with the current Unix time.
135 |

See RFC 9562.

136 |

137 | Primary function

138 |
static inline int8_t uuidv7_generate(uint8_t *uuid_out, uint64_t unix_ts_ms,
139 |
const uint8_t *rand_bytes,
140 |
const uint8_t *uuid_prev);
141 |

Generates a new UUIDv7 from the given Unix time, random bytes, and previous UUID.

142 |
    143 |
  • Parameters:
      144 |
    • uuid_out: 16-byte byte array where the generated UUID is stored.
    • 145 |
    • unix_ts_ms: Current Unix time in milliseconds.
    • 146 |
    • rand_bytes: At least 10-byte byte array filled with random bytes. This function consumes the leading 4 bytes or the whole 10 bytes per call depending on the conditions. uuidv7_status_n_rand_consumed() maps the return value of this function to the number of random bytes consumed.
    • 147 |
    • uuid_prev: 16-byte byte array representing the immediately preceding UUID, from which the previous timestamp and counter are extracted. This may be NULL if the caller does not care the ascending order of UUIDs within the same timestamp. This may point to the same location as uuid_out; this function reads the value before writing.
    • 148 |
    149 |
  • 150 |
  • Returns:
      151 |
    • One of the UUIDV7_STATUS_* codes that describe the characteristics of generated UUIDs. Callers can usually ignore the status unless they need to guarantee the monotonic order of UUIDs or fine-tune the generation process.
    • 152 |
    153 |
  • 154 |
155 |

See API reference for the full list of provided functions.

156 |

157 | Field and bit layout

158 |

This implementation produces identifiers with the following bit layout:

159 |
0 1 2 3
160 |
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
161 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
162 |
| unix_ts_ms |
163 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
164 |
| unix_ts_ms | ver | counter |
165 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
166 |
|var| counter |
167 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
168 |
| rand |
169 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
170 |

Where:

171 |
    172 |
  • The 48-bit unix_ts_ms field is dedicated to the Unix timestamp in milliseconds.
  • 173 |
  • The 4-bit ver field is set at 0111.
  • 174 |
  • The 42-bit counter field accommodates a counter that ensures the increasing order of IDs generated within a millisecond. The counter is incremented by one for each new ID and is reset to a random number when the unix_ts_ms changes.
  • 175 |
  • The 2-bit var field is set at 10.
  • 176 |
  • The remaining 32 rand bits are filled with a random number.
  • 177 |
178 |

The 42-bit counter is sufficiently large, so you do not usually need to worry about overflow, but in an extremely rare circumstance where it overflows, this library increments the unix_ts_ms field to continue instant monotonic generation. As a result, the generated ID may have a greater unix_ts_ms value than that passed as the argument. (See also Why so large counter? (42bits)).

179 |

UUIDv7, by design, relies on the system clock to guarantee the monotonically increasing order of generated IDs. A generator may not be able to produce a monotonic sequence if the system clock goes backwards. This library ignores a rollback of provided unix_ts_ms and reuses the one in uuid_prev unless the rollback is considered significant (namely, more than ten seconds). If such a significant rollback takes place, this library ignores the uuid_prev argument and thus breaks the increasing order of generated IDs.

180 |

181 | License

182 |

Licensed under the Apache License, Version 2.0.

183 |
184 | 185 |
186 | 187 | 190 |
191 | 192 | 193 | -------------------------------------------------------------------------------- /docs/menu.js: -------------------------------------------------------------------------------- 1 | /* 2 | @licstart The following is the entire license notice for the JavaScript code in this file. 3 | 4 | The MIT License (MIT) 5 | 6 | Copyright (C) 1997-2020 by Dimitri van Heesch 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software 9 | and associated documentation files (the "Software"), to deal in the Software without restriction, 10 | including without limitation the rights to use, copy, modify, merge, publish, distribute, 11 | sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in all copies or 15 | substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 18 | BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 20 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | @licend The above is the entire license notice for the JavaScript code in this file 24 | */ 25 | function initMenu(relPath,searchEnabled,serverSide,searchPage,search,treeview) { 26 | function makeTree(data,relPath) { 27 | let result=''; 28 | if ('children' in data) { 29 | result+='
    '; 30 | for (let i in data.children) { 31 | let url; 32 | const link = data.children[i].url; 33 | if (link.substring(0,1)=='^') { 34 | url = link.substring(1); 35 | } else { 36 | url = relPath+link; 37 | } 38 | result+='
  • '+ 39 | data.children[i].text+''+ 40 | makeTree(data.children[i],relPath)+'
  • '; 41 | } 42 | result+='
'; 43 | } 44 | return result; 45 | } 46 | let searchBoxHtml; 47 | if (searchEnabled) { 48 | if (serverSide) { 49 | searchBoxHtml='
'+ 50 | '
'+ 51 | '
 '+ 53 | ''+ 56 | '
'+ 57 | '
'+ 58 | '
'+ 59 | '
'; 60 | } else { 61 | searchBoxHtml='
'+ 62 | ''+ 63 | ' '+ 65 | ''+ 69 | ''+ 70 | ''+ 72 | ''+ 74 | ''+ 75 | '
'; 76 | } 77 | } 78 | 79 | $('#main-nav').before('
'+ 80 | ''+ 83 | ''+ 84 | '
'); 85 | $('#main-nav').append(makeTree(menudata,relPath)); 86 | $('#main-nav').children(':first').addClass('sm sm-dox').attr('id','main-menu'); 87 | if (searchBoxHtml) { 88 | $('#main-menu').append('
  • '); 89 | } 90 | const $mainMenuState = $('#main-menu-state'); 91 | let prevWidth = 0; 92 | if ($mainMenuState.length) { 93 | const initResizableIfExists = function() { 94 | if (typeof initResizable==='function') initResizable(treeview); 95 | } 96 | // animate mobile menu 97 | $mainMenuState.change(function() { 98 | const $menu = $('#main-menu'); 99 | let options = { duration: 250, step: initResizableIfExists }; 100 | if (this.checked) { 101 | options['complete'] = () => $menu.css('display', 'block'); 102 | $menu.hide().slideDown(options); 103 | } else { 104 | options['complete'] = () => $menu.css('display', 'none'); 105 | $menu.show().slideUp(options); 106 | } 107 | }); 108 | // set default menu visibility 109 | const resetState = function() { 110 | const $menu = $('#main-menu'); 111 | const newWidth = $(window).outerWidth(); 112 | if (newWidth!=prevWidth) { 113 | if ($(window).outerWidth()<768) { 114 | $mainMenuState.prop('checked',false); $menu.hide(); 115 | $('#searchBoxPos1').html(searchBoxHtml); 116 | $('#searchBoxPos2').hide(); 117 | } else { 118 | $menu.show(); 119 | $('#searchBoxPos1').empty(); 120 | $('#searchBoxPos2').html(searchBoxHtml); 121 | $('#searchBoxPos2').show(); 122 | } 123 | if (typeof searchBox!=='undefined') { 124 | searchBox.CloseResultsWindow(); 125 | } 126 | prevWidth = newWidth; 127 | } 128 | } 129 | $(window).ready(function() { resetState(); initResizableIfExists(); }); 130 | $(window).resize(resetState); 131 | } 132 | $('#main-menu').smartmenus(); 133 | } 134 | /* @license-end */ 135 | -------------------------------------------------------------------------------- /docs/menudata.js: -------------------------------------------------------------------------------- 1 | /* 2 | @licstart The following is the entire license notice for the JavaScript code in this file. 3 | 4 | The MIT License (MIT) 5 | 6 | Copyright (C) 1997-2020 by Dimitri van Heesch 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software 9 | and associated documentation files (the "Software"), to deal in the Software without restriction, 10 | including without limitation the rights to use, copy, modify, merge, publish, distribute, 11 | sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in all copies or 15 | substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 18 | BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 20 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | @licend The above is the entire license notice for the JavaScript code in this file 24 | */ 25 | var menudata={children:[ 26 | {text:"Main Page",url:"index.html"}, 27 | {text:"Files",url:"files.html",children:[ 28 | {text:"File List",url:"files.html"}, 29 | {text:"Globals",url:"globals.html",children:[ 30 | {text:"All",url:"globals.html"}, 31 | {text:"Functions",url:"globals_func.html"}, 32 | {text:"Macros",url:"globals_defs.html"}]}]}]} 33 | -------------------------------------------------------------------------------- /docs/minus.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /docs/minusd.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /docs/nav_f.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiosK/uuidv7-h/9ae44fec0eb2843d5230caac484b0388c93b836b/docs/nav_f.png -------------------------------------------------------------------------------- /docs/nav_fd.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiosK/uuidv7-h/9ae44fec0eb2843d5230caac484b0388c93b836b/docs/nav_fd.png -------------------------------------------------------------------------------- /docs/nav_g.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiosK/uuidv7-h/9ae44fec0eb2843d5230caac484b0388c93b836b/docs/nav_g.png -------------------------------------------------------------------------------- /docs/nav_h.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiosK/uuidv7-h/9ae44fec0eb2843d5230caac484b0388c93b836b/docs/nav_h.png -------------------------------------------------------------------------------- /docs/nav_hd.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiosK/uuidv7-h/9ae44fec0eb2843d5230caac484b0388c93b836b/docs/nav_hd.png -------------------------------------------------------------------------------- /docs/navtree.css: -------------------------------------------------------------------------------- 1 | #nav-tree .children_ul { 2 | margin:0; 3 | padding:4px; 4 | } 5 | 6 | #nav-tree ul { 7 | list-style:none outside none; 8 | margin:0px; 9 | padding:0px; 10 | } 11 | 12 | #nav-tree li { 13 | white-space:nowrap; 14 | margin:0px; 15 | padding:0px; 16 | } 17 | 18 | #nav-tree .plus { 19 | margin:0px; 20 | } 21 | 22 | #nav-tree .selected { 23 | background-image: url('tab_a.png'); 24 | background-repeat:repeat-x; 25 | color: var(--nav-text-active-color); 26 | text-shadow: var(--nav-text-active-shadow); 27 | } 28 | 29 | #nav-tree .selected .arrow { 30 | color: var(--nav-arrow-selected-color); 31 | text-shadow: none; 32 | } 33 | 34 | #nav-tree img { 35 | margin:0px; 36 | padding:0px; 37 | border:0px; 38 | vertical-align: middle; 39 | } 40 | 41 | #nav-tree a { 42 | text-decoration:none; 43 | padding:0px; 44 | margin:0px; 45 | } 46 | 47 | #nav-tree .label { 48 | margin:0px; 49 | padding:0px; 50 | font: 12px var(--font-family-nav); 51 | } 52 | 53 | #nav-tree .label a { 54 | padding:2px; 55 | } 56 | 57 | #nav-tree .selected a { 58 | text-decoration:none; 59 | color:var(--nav-text-active-color); 60 | } 61 | 62 | #nav-tree .children_ul { 63 | margin:0px; 64 | padding:0px; 65 | } 66 | 67 | #nav-tree .item { 68 | margin:0px; 69 | padding:0px; 70 | } 71 | 72 | #nav-tree { 73 | padding: 0px 0px; 74 | font-size:14px; 75 | overflow:auto; 76 | } 77 | 78 | #doc-content { 79 | overflow:auto; 80 | display:block; 81 | padding:0px; 82 | margin:0px; 83 | -webkit-overflow-scrolling : touch; /* iOS 5+ */ 84 | } 85 | 86 | #side-nav { 87 | padding:0 6px 0 0; 88 | margin: 0px; 89 | display:block; 90 | position: absolute; 91 | left: 0px; 92 | width: $width; 93 | overflow : hidden; 94 | } 95 | 96 | .ui-resizable .ui-resizable-handle { 97 | display:block; 98 | } 99 | 100 | .ui-resizable-e { 101 | background-image:var(--nav-splitbar-image); 102 | background-size:100%; 103 | background-repeat:repeat-y; 104 | background-attachment: scroll; 105 | cursor:ew-resize; 106 | height:100%; 107 | right:0; 108 | top:0; 109 | width:6px; 110 | } 111 | 112 | .ui-resizable-handle { 113 | display:none; 114 | font-size:0.1px; 115 | position:absolute; 116 | z-index:1; 117 | } 118 | 119 | #nav-tree-contents { 120 | margin: 6px 0px 0px 0px; 121 | } 122 | 123 | #nav-tree { 124 | background-repeat:repeat-x; 125 | background-color: var(--nav-background-color); 126 | -webkit-overflow-scrolling : touch; /* iOS 5+ */ 127 | } 128 | 129 | #nav-sync { 130 | position:absolute; 131 | top:5px; 132 | right:24px; 133 | z-index:0; 134 | } 135 | 136 | #nav-sync img { 137 | opacity:0.3; 138 | } 139 | 140 | #nav-sync img:hover { 141 | opacity:0.9; 142 | } 143 | 144 | @media print 145 | { 146 | #nav-tree { display: none; } 147 | div.ui-resizable-handle { display: none; position: relative; } 148 | } 149 | 150 | -------------------------------------------------------------------------------- /docs/open.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiosK/uuidv7-h/9ae44fec0eb2843d5230caac484b0388c93b836b/docs/open.png -------------------------------------------------------------------------------- /docs/plus.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /docs/plusd.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /docs/resize.js: -------------------------------------------------------------------------------- 1 | /* 2 | @licstart The following is the entire license notice for the JavaScript code in this file. 3 | 4 | The MIT License (MIT) 5 | 6 | Copyright (C) 1997-2020 by Dimitri van Heesch 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software 9 | and associated documentation files (the "Software"), to deal in the Software without restriction, 10 | including without limitation the rights to use, copy, modify, merge, publish, distribute, 11 | sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in all copies or 15 | substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 18 | BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 20 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | @licend The above is the entire license notice for the JavaScript code in this file 24 | */ 25 | 26 | function initResizable(treeview) { 27 | let sidenav,navtree,content,header,footer,barWidth=6; 28 | const RESIZE_COOKIE_NAME = ''+'width'; 29 | 30 | function resizeWidth() { 31 | const sidenavWidth = $(sidenav).outerWidth(); 32 | content.css({marginLeft:parseInt(sidenavWidth)+"px"}); 33 | if (typeof page_layout!=='undefined' && page_layout==1) { 34 | footer.css({marginLeft:parseInt(sidenavWidth)+"px"}); 35 | } 36 | Cookie.writeSetting(RESIZE_COOKIE_NAME,sidenavWidth-barWidth); 37 | } 38 | 39 | function restoreWidth(navWidth) { 40 | content.css({marginLeft:parseInt(navWidth)+barWidth+"px"}); 41 | if (typeof page_layout!=='undefined' && page_layout==1) { 42 | footer.css({marginLeft:parseInt(navWidth)+barWidth+"px"}); 43 | } 44 | sidenav.css({width:navWidth + "px"}); 45 | } 46 | 47 | function resizeHeight(treeview) { 48 | const headerHeight = header.outerHeight(); 49 | const windowHeight = $(window).height(); 50 | let contentHeight; 51 | if (treeview) 52 | { 53 | const footerHeight = footer.outerHeight(); 54 | let navtreeHeight,sideNavHeight; 55 | if (typeof page_layout==='undefined' || page_layout==0) { /* DISABLE_INDEX=NO */ 56 | contentHeight = windowHeight - headerHeight - footerHeight; 57 | navtreeHeight = contentHeight; 58 | sideNavHeight = contentHeight; 59 | } else if (page_layout==1) { /* DISABLE_INDEX=YES */ 60 | contentHeight = windowHeight - footerHeight; 61 | navtreeHeight = windowHeight - headerHeight; 62 | sideNavHeight = windowHeight; 63 | } 64 | navtree.css({height:navtreeHeight + "px"}); 65 | sidenav.css({height:sideNavHeight + "px"}); 66 | } 67 | else 68 | { 69 | contentHeight = windowHeight - headerHeight; 70 | } 71 | content.css({height:contentHeight + "px"}); 72 | if (location.hash.slice(1)) { 73 | (document.getElementById(location.hash.slice(1))||document.body).scrollIntoView(); 74 | } 75 | } 76 | 77 | function collapseExpand() { 78 | let newWidth; 79 | if (sidenav.width()>0) { 80 | newWidth=0; 81 | } else { 82 | const width = Cookie.readSetting(RESIZE_COOKIE_NAME,250); 83 | newWidth = (width>250 && width<$(window).width()) ? width : 250; 84 | } 85 | restoreWidth(newWidth); 86 | const sidenavWidth = $(sidenav).outerWidth(); 87 | Cookie.writeSetting(RESIZE_COOKIE_NAME,sidenavWidth-barWidth); 88 | } 89 | 90 | header = $("#top"); 91 | content = $("#doc-content"); 92 | footer = $("#nav-path"); 93 | sidenav = $("#side-nav"); 94 | if (!treeview) { 95 | // title = $("#titlearea"); 96 | // titleH = $(title).height(); 97 | // let animating = false; 98 | // content.on("scroll", function() { 99 | // slideOpts = { duration: 200, 100 | // step: function() { 101 | // contentHeight = $(window).height() - header.outerHeight(); 102 | // content.css({ height : contentHeight + "px" }); 103 | // }, 104 | // done: function() { animating=false; } 105 | // }; 106 | // if (content.scrollTop()>titleH && title.css('display')!='none' && !animating) { 107 | // title.slideUp(slideOpts); 108 | // animating=true; 109 | // } else if (content.scrollTop()<=titleH && title.css('display')=='none' && !animating) { 110 | // title.slideDown(slideOpts); 111 | // animating=true; 112 | // } 113 | // }); 114 | } else { 115 | navtree = $("#nav-tree"); 116 | $(".side-nav-resizable").resizable({resize: function(e, ui) { resizeWidth(); } }); 117 | $(sidenav).resizable({ minWidth: 0 }); 118 | } 119 | $(window).resize(function() { resizeHeight(treeview); }); 120 | if (treeview) 121 | { 122 | const device = navigator.userAgent.toLowerCase(); 123 | const touch_device = device.match(/(iphone|ipod|ipad|android)/); 124 | if (touch_device) { /* wider split bar for touch only devices */ 125 | $(sidenav).css({ paddingRight:'20px' }); 126 | $('.ui-resizable-e').css({ width:'20px' }); 127 | $('#nav-sync').css({ right:'34px' }); 128 | barWidth=20; 129 | } 130 | const width = Cookie.readSetting(RESIZE_COOKIE_NAME,250); 131 | if (width) { restoreWidth(width); } else { resizeWidth(); } 132 | } 133 | resizeHeight(treeview); 134 | const url = location.href; 135 | const i=url.indexOf("#"); 136 | if (i>=0) window.location.hash=url.substr(i); 137 | const _preventDefault = function(evt) { evt.preventDefault(); }; 138 | if (treeview) 139 | { 140 | $("#splitbar").bind("dragstart", _preventDefault).bind("selectstart", _preventDefault); 141 | $(".ui-resizable-handle").dblclick(collapseExpand); 142 | // workaround for firefox 143 | $("body").css({overflow: "hidden"}); 144 | } 145 | $(window).on('load',function() { resizeHeight(treeview); }); 146 | } 147 | /* @license-end */ 148 | -------------------------------------------------------------------------------- /docs/splitbar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiosK/uuidv7-h/9ae44fec0eb2843d5230caac484b0388c93b836b/docs/splitbar.png -------------------------------------------------------------------------------- /docs/splitbard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiosK/uuidv7-h/9ae44fec0eb2843d5230caac484b0388c93b836b/docs/splitbard.png -------------------------------------------------------------------------------- /docs/sync_off.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiosK/uuidv7-h/9ae44fec0eb2843d5230caac484b0388c93b836b/docs/sync_off.png -------------------------------------------------------------------------------- /docs/sync_on.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiosK/uuidv7-h/9ae44fec0eb2843d5230caac484b0388c93b836b/docs/sync_on.png -------------------------------------------------------------------------------- /docs/tab_a.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiosK/uuidv7-h/9ae44fec0eb2843d5230caac484b0388c93b836b/docs/tab_a.png -------------------------------------------------------------------------------- /docs/tab_ad.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiosK/uuidv7-h/9ae44fec0eb2843d5230caac484b0388c93b836b/docs/tab_ad.png -------------------------------------------------------------------------------- /docs/tab_b.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiosK/uuidv7-h/9ae44fec0eb2843d5230caac484b0388c93b836b/docs/tab_b.png -------------------------------------------------------------------------------- /docs/tab_bd.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiosK/uuidv7-h/9ae44fec0eb2843d5230caac484b0388c93b836b/docs/tab_bd.png -------------------------------------------------------------------------------- /docs/tab_h.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiosK/uuidv7-h/9ae44fec0eb2843d5230caac484b0388c93b836b/docs/tab_h.png -------------------------------------------------------------------------------- /docs/tab_hd.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiosK/uuidv7-h/9ae44fec0eb2843d5230caac484b0388c93b836b/docs/tab_hd.png -------------------------------------------------------------------------------- /docs/tab_s.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiosK/uuidv7-h/9ae44fec0eb2843d5230caac484b0388c93b836b/docs/tab_s.png -------------------------------------------------------------------------------- /docs/tab_sd.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiosK/uuidv7-h/9ae44fec0eb2843d5230caac484b0388c93b836b/docs/tab_sd.png -------------------------------------------------------------------------------- /docs/tabs.css: -------------------------------------------------------------------------------- 1 | .sm{position:relative;z-index:9999}.sm,.sm ul,.sm li{display:block;list-style:none;margin:0;padding:0;line-height:normal;direction:ltr;text-align:left;-webkit-tap-highlight-color:rgba(0,0,0,0)}.sm-rtl,.sm-rtl ul,.sm-rtl li{direction:rtl;text-align:right}.sm>li>h1,.sm>li>h2,.sm>li>h3,.sm>li>h4,.sm>li>h5,.sm>li>h6{margin:0;padding:0}.sm ul{display:none}.sm li,.sm a{position:relative}.sm a{display:block}.sm a.disabled{cursor:not-allowed}.sm:after{content:"\00a0";display:block;height:0;font:0/0 serif;clear:both;visibility:hidden;overflow:hidden}.sm,.sm *,.sm *:before,.sm *:after{-moz-box-sizing:border-box;-webkit-box-sizing:border-box;box-sizing:border-box}.main-menu-btn{position:relative;display:inline-block;width:36px;height:36px;text-indent:36px;margin-left:8px;white-space:nowrap;overflow:hidden;cursor:pointer;-webkit-tap-highlight-color:rgba(0,0,0,0)}.main-menu-btn-icon,.main-menu-btn-icon:before,.main-menu-btn-icon:after{position:absolute;top:50%;left:2px;height:2px;width:24px;background:var(--nav-menu-button-color);-webkit-transition:all .25s;transition:all .25s}.main-menu-btn-icon:before{content:'';top:-7px;left:0}.main-menu-btn-icon:after{content:'';top:7px;left:0}#main-menu-state:checked ~ .main-menu-btn .main-menu-btn-icon{height:0}#main-menu-state:checked ~ .main-menu-btn .main-menu-btn-icon:before{top:0;-webkit-transform:rotate(-45deg);transform:rotate(-45deg)}#main-menu-state:checked ~ .main-menu-btn .main-menu-btn-icon:after{top:0;-webkit-transform:rotate(45deg);transform:rotate(45deg)}#main-menu-state{position:absolute;width:1px;height:1px;margin:-1px;border:0;padding:0;overflow:hidden;clip:rect(1px,1px,1px,1px)}#main-menu-state:not(:checked) ~ #main-menu{display:none}#main-menu-state:checked ~ #main-menu{display:block}@media(min-width:768px){.main-menu-btn{position:absolute;top:-99999px}#main-menu-state:not(:checked) ~ #main-menu{display:block}}.sm-dox{background-image:var(--nav-gradient-image)}.sm-dox a,.sm-dox a:focus,.sm-dox a:hover,.sm-dox a:active{padding:0 12px;padding-right:43px;font-family:var(--font-family-nav);font-size:13px;font-weight:bold;line-height:36px;text-decoration:none;text-shadow:var(--nav-text-normal-shadow);color:var(--nav-text-normal-color);outline:0}.sm-dox a:hover{background-image:var(--nav-gradient-active-image);background-repeat:repeat-x;color:var(--nav-text-hover-color);text-shadow:var(--nav-text-hover-shadow)}.sm-dox a.current{color:#d23600}.sm-dox a.disabled{color:#bbb}.sm-dox a span.sub-arrow{position:absolute;top:50%;margin-top:-14px;left:auto;right:3px;width:28px;height:28px;overflow:hidden;font:bold 12px/28px monospace !important;text-align:center;text-shadow:none;background:var(--nav-menu-toggle-color);-moz-border-radius:5px;-webkit-border-radius:5px;border-radius:5px}.sm-dox a span.sub-arrow:before{display:block;content:'+'}.sm-dox a.highlighted span.sub-arrow:before{display:block;content:'-'}.sm-dox>li:first-child>a,.sm-dox>li:first-child>:not(ul) a{-moz-border-radius:5px 5px 0 0;-webkit-border-radius:5px;border-radius:5px 5px 0 0}.sm-dox>li:last-child>a,.sm-dox>li:last-child>*:not(ul) a,.sm-dox>li:last-child>ul,.sm-dox>li:last-child>ul>li:last-child>a,.sm-dox>li:last-child>ul>li:last-child>*:not(ul) a,.sm-dox>li:last-child>ul>li:last-child>ul,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>a,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>*:not(ul) a,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>ul,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>a,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>*:not(ul) a,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>ul,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>a,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>*:not(ul) a,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>ul{-moz-border-radius:0 0 5px 5px;-webkit-border-radius:0;border-radius:0 0 5px 5px}.sm-dox>li:last-child>a.highlighted,.sm-dox>li:last-child>*:not(ul) a.highlighted,.sm-dox>li:last-child>ul>li:last-child>a.highlighted,.sm-dox>li:last-child>ul>li:last-child>*:not(ul) a.highlighted,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>a.highlighted,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>*:not(ul) a.highlighted,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>a.highlighted,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>*:not(ul) a.highlighted,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>a.highlighted,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>*:not(ul) a.highlighted{-moz-border-radius:0;-webkit-border-radius:0;border-radius:0}.sm-dox ul{background:var(--nav-menu-background-color)}.sm-dox ul a,.sm-dox ul a:focus,.sm-dox ul a:hover,.sm-dox ul a:active{font-size:12px;border-left:8px solid transparent;line-height:36px;text-shadow:none;background-color:var(--nav-menu-background-color);background-image:none}.sm-dox ul a:hover{background-image:var(--nav-gradient-active-image);background-repeat:repeat-x;color:var(--nav-text-hover-color);text-shadow:0 1px 1px black}.sm-dox ul ul a,.sm-dox ul ul a:hover,.sm-dox ul ul a:focus,.sm-dox ul ul a:active{border-left:16px solid transparent}.sm-dox ul ul ul a,.sm-dox ul ul ul a:hover,.sm-dox ul ul ul a:focus,.sm-dox ul ul ul a:active{border-left:24px solid transparent}.sm-dox ul ul ul ul a,.sm-dox ul ul ul ul a:hover,.sm-dox ul ul ul ul a:focus,.sm-dox ul ul ul ul a:active{border-left:32px solid transparent}.sm-dox ul ul ul ul ul a,.sm-dox ul ul ul ul ul a:hover,.sm-dox ul ul ul ul ul a:focus,.sm-dox ul ul ul ul ul a:active{border-left:40px solid transparent}@media(min-width:768px){.sm-dox ul{position:absolute;width:12em}.sm-dox li{float:left}.sm-dox.sm-rtl li{float:right}.sm-dox ul li,.sm-dox.sm-rtl ul li,.sm-dox.sm-vertical li{float:none}.sm-dox a{white-space:nowrap}.sm-dox ul a,.sm-dox.sm-vertical a{white-space:normal}.sm-dox .sm-nowrap>li>a,.sm-dox .sm-nowrap>li>:not(ul) a{white-space:nowrap}.sm-dox{padding:0 10px;background-image:var(--nav-gradient-image);line-height:36px}.sm-dox a span.sub-arrow{top:50%;margin-top:-2px;right:12px;width:0;height:0;border-width:4px;border-style:solid dashed dashed dashed;border-color:var(--nav-text-normal-color) transparent transparent transparent;background:transparent;-moz-border-radius:0;-webkit-border-radius:0;border-radius:0}.sm-dox a,.sm-dox a:focus,.sm-dox a:active,.sm-dox a:hover,.sm-dox a.highlighted{padding:0 12px;background-image:var(--nav-separator-image);background-repeat:no-repeat;background-position:right;-moz-border-radius:0 !important;-webkit-border-radius:0;border-radius:0 !important}.sm-dox a:hover{background-image:var(--nav-gradient-active-image);background-repeat:repeat-x;color:var(--nav-text-hover-color);text-shadow:var(--nav-text-hover-shadow)}.sm-dox a:hover span.sub-arrow{border-color:var(--nav-text-hover-color) transparent transparent transparent}.sm-dox a.has-submenu{padding-right:24px}.sm-dox li{border-top:0}.sm-dox>li>ul:before,.sm-dox>li>ul:after{content:'';position:absolute;top:-18px;left:30px;width:0;height:0;overflow:hidden;border-width:9px;border-style:dashed dashed solid dashed;border-color:transparent transparent #bbb transparent}.sm-dox>li>ul:after{top:-16px;left:31px;border-width:8px;border-color:transparent transparent var(--nav-menu-background-color) transparent}.sm-dox ul{border:1px solid #bbb;padding:5px 0;background:var(--nav-menu-background-color);-moz-border-radius:5px !important;-webkit-border-radius:5px;border-radius:5px !important;-moz-box-shadow:0 5px 9px rgba(0,0,0,0.2);-webkit-box-shadow:0 5px 9px rgba(0,0,0,0.2);box-shadow:0 5px 9px rgba(0,0,0,0.2)}.sm-dox ul a span.sub-arrow{right:8px;top:50%;margin-top:-5px;border-width:5px;border-color:transparent transparent transparent var(--nav-menu-foreground-color);border-style:dashed dashed dashed solid}.sm-dox ul a,.sm-dox ul a:hover,.sm-dox ul a:focus,.sm-dox ul a:active,.sm-dox ul a.highlighted{color:var(--nav-menu-foreground-color);background-image:none;border:0 !important}.sm-dox ul a:hover{background-image:var(--nav-gradient-active-image);background-repeat:repeat-x;color:var(--nav-text-hover-color);text-shadow:var(--nav-text-hover-shadow)}.sm-dox ul a:hover span.sub-arrow{border-color:transparent transparent transparent var(--nav-text-hover-color)}.sm-dox span.scroll-up,.sm-dox span.scroll-down{position:absolute;display:none;visibility:hidden;overflow:hidden;background:var(--nav-menu-background-color);height:36px}.sm-dox span.scroll-up:hover,.sm-dox span.scroll-down:hover{background:#eee}.sm-dox span.scroll-up:hover span.scroll-up-arrow,.sm-dox span.scroll-up:hover span.scroll-down-arrow{border-color:transparent transparent #d23600 transparent}.sm-dox span.scroll-down:hover span.scroll-down-arrow{border-color:#d23600 transparent transparent transparent}.sm-dox span.scroll-up-arrow,.sm-dox span.scroll-down-arrow{position:absolute;top:0;left:50%;margin-left:-6px;width:0;height:0;overflow:hidden;border-width:6px;border-style:dashed dashed solid dashed;border-color:transparent transparent var(--nav-menu-foreground-color) transparent}.sm-dox span.scroll-down-arrow{top:8px;border-style:solid dashed dashed dashed;border-color:var(--nav-menu-foreground-color) transparent transparent transparent}.sm-dox.sm-rtl a.has-submenu{padding-right:12px;padding-left:24px}.sm-dox.sm-rtl a span.sub-arrow{right:auto;left:12px}.sm-dox.sm-rtl.sm-vertical a.has-submenu{padding:10px 20px}.sm-dox.sm-rtl.sm-vertical a span.sub-arrow{right:auto;left:8px;border-style:dashed solid dashed dashed;border-color:transparent #555 transparent transparent}.sm-dox.sm-rtl>li>ul:before{left:auto;right:30px}.sm-dox.sm-rtl>li>ul:after{left:auto;right:31px}.sm-dox.sm-rtl ul a.has-submenu{padding:10px 20px !important}.sm-dox.sm-rtl ul a span.sub-arrow{right:auto;left:8px;border-style:dashed solid dashed dashed;border-color:transparent #555 transparent transparent}.sm-dox.sm-vertical{padding:10px 0;-moz-border-radius:5px;-webkit-border-radius:5px;border-radius:5px}.sm-dox.sm-vertical a{padding:10px 20px}.sm-dox.sm-vertical a:hover,.sm-dox.sm-vertical a:focus,.sm-dox.sm-vertical a:active,.sm-dox.sm-vertical a.highlighted{background:#fff}.sm-dox.sm-vertical a.disabled{background-image:var(--nav-gradient-image)}.sm-dox.sm-vertical a span.sub-arrow{right:8px;top:50%;margin-top:-5px;border-width:5px;border-style:dashed dashed dashed solid;border-color:transparent transparent transparent #555}.sm-dox.sm-vertical>li>ul:before,.sm-dox.sm-vertical>li>ul:after{display:none}.sm-dox.sm-vertical ul a{padding:10px 20px}.sm-dox.sm-vertical ul a:hover,.sm-dox.sm-vertical ul a:focus,.sm-dox.sm-vertical ul a:active,.sm-dox.sm-vertical ul a.highlighted{background:#eee}.sm-dox.sm-vertical ul a.disabled{background:var(--nav-menu-background-color)}} 2 | -------------------------------------------------------------------------------- /docs/uuidv7_8h.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | uuidv7.h: uuidv7.h File Reference 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
    19 |
    20 | 21 | 22 | 23 | 27 | 28 | 29 |
    24 |
    uuidv7.h 25 |
    26 |
    30 |
    31 | 32 | 33 | 38 | 39 | 40 | 47 | 48 | 53 |
    54 |
    55 |
    56 |
    uuidv7.h File Reference
    57 |
    58 |
    59 | 60 |

    uuidv7.h - Single-file C/C++ UUIDv7 Library 61 | More...

    62 |
    #include <stddef.h>
    63 | #include <stdint.h>
    64 |
    65 |

    Go to the source code of this file.

    66 | 67 | 69 | 70 | 72 | 73 | 74 | 76 | 77 | 78 | 80 | 81 | 82 | 84 | 85 | 86 | 88 | 89 | 90 | 92 | 93 | 94 | 96 | 97 | 98 |

    68 | Macros

    Status codes returned by uuidv7_generate()
    71 | #define UUIDV7_STATUS_UNPRECEDENTED   (0)
     Indicates that the unix_ts_ms passed was used because no preceding UUID was specified.
     
    75 | #define UUIDV7_STATUS_NEW_TIMESTAMP   (1)
     Indicates that the unix_ts_ms passed was used because it was greater than the previous one.
     
    79 | #define UUIDV7_STATUS_COUNTER_INC   (2)
     Indicates that the counter was incremented because the unix_ts_ms passed was no greater than the previous one.
     
    83 | #define UUIDV7_STATUS_TIMESTAMP_INC   (3)
     Indicates that the previous unix_ts_ms was incremented because the counter reached its maximum value.
     
    87 | #define UUIDV7_STATUS_CLOCK_ROLLBACK   (4)
     Indicates that the monotonic order of generated UUIDs was broken because the unix_ts_ms passed was less than the previous one by more than ten seconds.
     
    91 | #define UUIDV7_STATUS_ERR_TIMESTAMP   (-1)
     Indicates that an invalid unix_ts_ms is passed.
     
    95 | #define UUIDV7_STATUS_ERR_TIMESTAMP_OVERFLOW   (-2)
     Indicates that the attempt to increment the previous unix_ts_ms failed because it had reached its maximum value.
     
    99 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 |

    100 | Functions

    Low-level primitives
    static int8_t uuidv7_generate (uint8_t *uuid_out, uint64_t unix_ts_ms, const uint8_t *rand_bytes, const uint8_t *uuid_prev)
     Generates a new UUIDv7 from the given Unix time, random bytes, and previous UUID.
     
    static int uuidv7_status_n_rand_consumed (int8_t status)
     Determines the number of random bytes consumsed by uuidv7_generate() from the UUIDV7_STATUS_* code returned.
     
    static void uuidv7_to_string (const uint8_t *uuid, char *string_out)
     Encodes a UUID in the 8-4-4-4-12 hexadecimal string representation.
     
    static int uuidv7_from_string (const char *string, uint8_t *uuid_out)
     Decodes the 8-4-4-4-12 hexadecimal string representation of a UUID.
     
    High-level APIs that require platform integration
    int uuidv7_new (uint8_t *uuid_out)
     Generates a new UUIDv7 with the current Unix time.
     
    static int uuidv7_new_string (char *string_out)
     Generates an 8-4-4-4-12 hexadecimal string representation of new UUIDv7.
     
    122 |

    Detailed Description

    123 |

    uuidv7.h - Single-file C/C++ UUIDv7 Library

    124 |
    Version
    v0.1.6
    125 |
    Author
    LiosK
    126 | 127 |
    See also
    https://github.com/LiosK/uuidv7-h
    128 |

    Function Documentation

    129 | 130 |

    ◆ uuidv7_from_string()

    131 | 132 |
    133 |
    134 | 135 | 136 | 150 | 152 | 153 |
    137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 |
    static int uuidv7_from_string (const char * string,
    uint8_t * uuid_out )
    149 |
    151 | inlinestatic
    154 |
    155 | 156 |

    Decodes the 8-4-4-4-12 hexadecimal string representation of a UUID.

    157 |
    Parameters
    158 | 159 | 160 | 161 |
    string37-byte (36 digits + NUL) character array representing the 8-4-4-4-12 hexadecimal string representation.
    uuid_out16-byte byte array where the decoded UUID is stored.
    162 |
    163 |
    164 |
    Returns
    Zero on success or non-zero integer on failure.
    165 | 166 |
    167 |
    168 | 169 |

    ◆ uuidv7_generate()

    170 | 171 |
    172 |
    173 | 174 | 175 | 199 | 201 | 202 |
    176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 |
    static int8_t uuidv7_generate (uint8_t * uuid_out,
    uint64_t unix_ts_ms,
    const uint8_t * rand_bytes,
    const uint8_t * uuid_prev )
    198 |
    200 | inlinestatic
    203 |
    204 | 205 |

    Generates a new UUIDv7 from the given Unix time, random bytes, and previous UUID.

    206 |
    Parameters
    207 | 208 | 209 | 210 | 211 | 212 |
    uuid_out16-byte byte array where the generated UUID is stored.
    unix_ts_msCurrent Unix time in milliseconds.
    rand_bytesAt least 10-byte byte array filled with random bytes. This function consumes the leading 4 bytes or the whole 10 bytes per call depending on the conditions. uuidv7_status_n_rand_consumed() maps the return value of this function to the number of random bytes consumed.
    uuid_prev16-byte byte array representing the immediately preceding UUID, from which the previous timestamp and counter are extracted. This may be NULL if the caller does not care the ascending order of UUIDs within the same timestamp. This may point to the same location as uuid_out; this function reads the value before writing.
    213 |
    214 |
    215 |
    Returns
    One of the UUIDV7_STATUS_* codes that describe the characteristics of generated UUIDs. Callers can usually ignore the status unless they need to guarantee the monotonic order of UUIDs or fine-tune the generation process.
    216 | 217 |
    218 |
    219 | 220 |

    ◆ uuidv7_new()

    221 | 222 |
    223 |
    224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 |
    int uuidv7_new (uint8_t * uuid_out)
    232 |
    233 | 234 |

    Generates a new UUIDv7 with the current Unix time.

    235 |

    This declaration defines the interface to generate a new UUIDv7 with the current time, default random number generator, and global shared state holding the previously generated UUID. Since this single-file library does not provide platform-specific implementations, users need to prepare a concrete implementation (if necessary) by integrating a real-time clock, cryptographically strong random number generator, and shared state storage available in the target platform.

    236 |
    Parameters
    237 | 238 | 239 |
    uuid_out16-byte byte array where the generated UUID is stored.
    240 |
    241 |
    242 |
    Returns
    One of the UUIDV7_STATUS_* codes that describe the characteristics of generated UUIDs or an implementation-dependent code. Callers can usually ignore the UUIDV7_STATUS_* code unless they need to guarantee the monotonic order of UUIDs or fine-tune the generation process. The implementation-dependent code must be out of the range of int8_t and negative if it reports an error.
    243 | 244 |
    245 |
    246 | 247 |

    ◆ uuidv7_new_string()

    248 | 249 |
    250 |
    251 | 252 | 253 | 263 | 265 | 266 |
    254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 |
    static int uuidv7_new_string (char * string_out)
    262 |
    264 | inlinestatic
    267 |
    268 | 269 |

    Generates an 8-4-4-4-12 hexadecimal string representation of new UUIDv7.

    270 |
    Parameters
    271 | 272 | 273 |
    string_outCharacter array where the encoded string is stored. Its length must be 37 (36 digits + NUL) or longer.
    274 |
    275 |
    276 |
    Returns
    Return value of uuidv7_new().
    277 |
    Note
    Provide a concrete uuidv7_new() implementation to enable this function.
    278 | 279 |
    280 |
    281 | 282 |

    ◆ uuidv7_status_n_rand_consumed()

    283 | 284 |
    285 |
    286 | 287 | 288 | 298 | 300 | 301 |
    289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 |
    static int uuidv7_status_n_rand_consumed (int8_t status)
    297 |
    299 | inlinestatic
    302 |
    303 | 304 |

    Determines the number of random bytes consumsed by uuidv7_generate() from the UUIDV7_STATUS_* code returned.

    305 |
    Parameters
    306 | 307 | 308 |
    statusUUIDV7_STATUS_* code returned by uuidv7_generate().
    309 |
    310 |
    311 |
    Returns
    4 if status is UUIDV7_STATUS_COUNTER_INC or 10 otherwise.
    312 | 313 |
    314 |
    315 | 316 |

    ◆ uuidv7_to_string()

    317 | 318 |
    319 |
    320 | 321 | 322 | 336 | 338 | 339 |
    323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 |
    static void uuidv7_to_string (const uint8_t * uuid,
    char * string_out )
    335 |
    337 | inlinestatic
    340 |
    341 | 342 |

    Encodes a UUID in the 8-4-4-4-12 hexadecimal string representation.

    343 |
    Parameters
    344 | 345 | 346 | 347 |
    uuid16-byte byte array representing the UUID to encode.
    string_outCharacter array where the encoded string is stored. Its length must be 37 (36 digits + NUL) or longer.
    348 |
    349 |
    350 | 351 |
    352 |
    353 |
    354 | 355 | 358 |
    359 | 360 | 361 | -------------------------------------------------------------------------------- /test/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -I.. -Wall -Wextra -pedantic-errors 2 | CXXFLAGS = -I.. -Wall -Wextra -pedantic-errors 3 | 4 | .PHONY: test test_core test_new_unix clean 5 | 6 | test: test_core test_new_unix 7 | 8 | test_core: test_core.c.out test_core.cxx.out 9 | ./test_core.c.out 10 | ./test_core.cxx.out 11 | 12 | test_new_unix: test_new_unix.c.out test_new_unix.cxx.out 13 | ./test_new_unix.c.out 14 | ./test_new_unix.cxx.out 15 | 16 | clean: 17 | $(RM) *.out 18 | 19 | test_core.c.out: test_core.c ../uuidv7.h 20 | $(CC) $(CFLAGS) -std=c99 -o$@ $< 21 | 22 | test_core.cxx.out: test_core.c ../uuidv7.h 23 | $(CXX) $(CXXFLAGS) -std=c++98 -o$@ $< 24 | 25 | test_new_%.c.out: impl_new_%.c test_new.c ../uuidv7.h 26 | $(CC) $(CFLAGS) -o$@ $< test_new.c 27 | 28 | test_new_%.cxx.out: impl_new_%.c test_new.c ../uuidv7.h 29 | $(CXX) $(CXXFLAGS) -o$@ $< test_new.c 30 | -------------------------------------------------------------------------------- /test/impl_new_unix.c: -------------------------------------------------------------------------------- 1 | #include "uuidv7.h" 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include // for macOS getentropy() 8 | 9 | int uuidv7_new(uint8_t *uuid_out) { 10 | static uint8_t uuid_prev[16] = {0}; 11 | static uint8_t rand_bytes[256] = {0}; 12 | static size_t n_rand_consumed = sizeof(rand_bytes); 13 | 14 | struct timespec tp; 15 | clock_gettime(CLOCK_REALTIME, &tp); 16 | uint64_t unix_ts_ms = (uint64_t)tp.tv_sec * 1000 + tp.tv_nsec / 1000000; 17 | 18 | if (n_rand_consumed > sizeof(rand_bytes) - 10) { 19 | getentropy(rand_bytes, n_rand_consumed); 20 | n_rand_consumed = 0; 21 | } 22 | 23 | int8_t status = uuidv7_generate(uuid_prev, unix_ts_ms, 24 | &rand_bytes[n_rand_consumed], uuid_prev); 25 | n_rand_consumed += uuidv7_status_n_rand_consumed(status); 26 | 27 | memcpy(uuid_out, uuid_prev, 16); 28 | return status; 29 | } 30 | -------------------------------------------------------------------------------- /test/test_core.c: -------------------------------------------------------------------------------- 1 | #include "uuidv7.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | void test_unprecedented(void) { 9 | struct TestCase { 10 | uint64_t unix_ts_ms; 11 | uint8_t rand_bytes[10]; 12 | char string[37]; 13 | } cases[] = {{0x0123456789ab, 14 | {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 15 | "01234567-89ab-7000-8000-000000000000"}, 16 | {0xba9876543210, 17 | {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 18 | "ba987654-3210-7fff-bfff-ffffffffffff"}, 19 | {0x17f22e279b0, 20 | {0xfc, 0xc3, 0x58, 0xc4, 0xdc, 0x0c, 0x0c, 0x07, 0x39, 0x8f}, 21 | "017f22e2-79b0-7cc3-98c4-dc0c0c07398f"}}; 22 | int n_cases = sizeof(cases) / sizeof(struct TestCase); 23 | 24 | struct TestCase *e = cases; 25 | for (int i = 0; i < n_cases; i++, e++) { 26 | uint8_t uuid[16]; 27 | int status = uuidv7_generate(uuid, e->unix_ts_ms, e->rand_bytes, NULL); 28 | assert(status == UUIDV7_STATUS_UNPRECEDENTED); 29 | assert(uuidv7_status_n_rand_consumed(status) == 10); 30 | 31 | char buffer[37]; 32 | uuidv7_to_string(uuid, buffer); 33 | assert(strcmp(buffer, e->string) == 0); 34 | } 35 | } 36 | 37 | void test_with_prev(void) { 38 | struct TestCase { 39 | uint64_t unix_ts_ms; 40 | uint8_t rand_bytes[10]; 41 | int8_t status; 42 | char string[37]; 43 | } cases[] = {{0x0123456789ab, 44 | {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 45 | UUIDV7_STATUS_NEW_TIMESTAMP, 46 | "01234567-89ab-7000-8000-000000000000"}, 47 | {0x17f22e279b0, 48 | {0xfc, 0xc3, 0x58, 0xc4, 0xdc, 0x0c, 0x0c, 0x07, 0x39, 0x8f}, 49 | UUIDV7_STATUS_NEW_TIMESTAMP, 50 | "017f22e2-79b0-7cc3-98c4-dc0c0c07398f"}, 51 | {0x17f22e279b0, 52 | {0xfc, 0xc3, 0x58, 0xc4, 0xdc, 0x0c, 0x0c, 0x07, 0x39, 0x8f}, 53 | UUIDV7_STATUS_COUNTER_INC, 54 | "017f22e2-79b0-7cc3-98c4-dc0dfcc358c4"}, 55 | {0x17f22e279b0, 56 | {0xfc, 0xc3, 0x58, 0xc4, 0xdc, 0x0c, 0x0c, 0x07, 0x39, 0x8f}, 57 | UUIDV7_STATUS_COUNTER_INC, 58 | "017f22e2-79b0-7cc3-98c4-dc0efcc358c4"}, 59 | {0x17f22e279b0 - 1, 60 | {0xfc, 0xc3, 0x58, 0xc4, 0xdc, 0x0c, 0x0c, 0x07, 0x39, 0x8f}, 61 | UUIDV7_STATUS_COUNTER_INC, 62 | "017f22e2-79b0-7cc3-98c4-dc0ffcc358c4"}, 63 | {0x17f22e279b0 - 10000, 64 | {0xfc, 0xc3, 0x58, 0xc4, 0xdc, 0x0c, 0x0c, 0x07, 0x39, 0x8f}, 65 | UUIDV7_STATUS_COUNTER_INC, 66 | "017f22e2-79b0-7cc3-98c4-dc10fcc358c4"}, 67 | {0x17f22e279b0 - 10001, 68 | {0xfc, 0xc3, 0x58, 0xc4, 0xdc, 0x0c, 0x0c, 0x07, 0x39, 0x8f}, 69 | UUIDV7_STATUS_CLOCK_ROLLBACK, 70 | "017f22e2-529f-7cc3-98c4-dc0c0c07398f"}, 71 | {0xba9876543210, 72 | {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 73 | UUIDV7_STATUS_NEW_TIMESTAMP, 74 | "ba987654-3210-7fff-bfff-ffffffffffff"}, 75 | {0xba9876543210, 76 | {0xfc, 0xc3, 0x58, 0xc4, 0xdc, 0x0c, 0x0c, 0x07, 0x39, 0x8f}, 77 | UUIDV7_STATUS_TIMESTAMP_INC, 78 | "ba987654-3211-7cc3-98c4-dc0c0c07398f"}, 79 | {0xba9876543210, 80 | {0xfc, 0xc3, 0x58, 0xc4, 0xdc, 0x0c, 0x0c, 0x07, 0x39, 0x8f}, 81 | UUIDV7_STATUS_COUNTER_INC, 82 | "ba987654-3211-7cc3-98c4-dc0dfcc358c4"}}; 83 | int n_cases = sizeof(cases) / sizeof(struct TestCase); 84 | 85 | uint8_t uuid[16] = {0}; 86 | struct TestCase *e = cases; 87 | for (int i = 0; i < n_cases; i++, e++) { 88 | int status = uuidv7_generate(uuid, e->unix_ts_ms, e->rand_bytes, uuid); 89 | assert(status == e->status); 90 | 91 | char buffer[37]; 92 | uuidv7_to_string(uuid, buffer); 93 | assert(strcmp(buffer, e->string) == 0); 94 | } 95 | } 96 | 97 | void test_from_to_string(void) { 98 | struct TestCase { 99 | uint8_t bytes[16]; 100 | char string[37]; 101 | } cases[] = {{{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0x70, 0x00, 0x80, 0x00, 102 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 103 | "01234567-89ab-7000-8000-000000000000"}, 104 | {{0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x7f, 0xff, 0xbf, 0xff, 105 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 106 | "ba987654-3210-7fff-bfff-ffffffffffff"}, 107 | {{0x01, 0x7f, 0x22, 0xe2, 0x79, 0xb0, 0x7c, 0xc3, 0x98, 0xc4, 108 | 0xdc, 0x0c, 0x0c, 0x07, 0x39, 0x8f}, 109 | "017f22e2-79b0-7cc3-98c4-dc0c0c07398f"}}; 110 | int n_cases = sizeof(cases) / sizeof(struct TestCase); 111 | 112 | struct TestCase *e = cases; 113 | for (int i = 0; i < n_cases; i++, e++) { 114 | char string[37]; 115 | uuidv7_to_string(e->bytes, string); 116 | assert(strcmp(string, e->string) == 0); 117 | 118 | uint8_t uuid[16]; 119 | int err = uuidv7_from_string(e->string, uuid); 120 | assert(err == 0); 121 | assert(memcmp(uuid, e->bytes, 16) == 0); 122 | 123 | for (int i = 0; e->string[i] != '\0'; i++) { 124 | string[i] = toupper(e->string[i]); 125 | } 126 | string[36] = '\0'; 127 | err = uuidv7_from_string(string, uuid); 128 | assert(err == 0); 129 | assert(memcmp(uuid, e->bytes, 16) == 0); 130 | } 131 | } 132 | 133 | void test_from_string_error(void) { 134 | char cases[][40] = { 135 | "", 136 | " 01815515-70af-73bb-a597-725c2086f131", 137 | "01815515-70af-73bb-a597-725d90351474 ", 138 | " 01815515-70af-73bb-a597-725eb688399d ", 139 | "+01815515-70af-73bb-a597-725f825e57d7", 140 | "-01815515-70af-73bb-a597-72602859e906", 141 | "+1815515-70af-73bb-a597-7261115ae9fd", 142 | "-1815515-70af-73bb-a597-7262c43b6483", 143 | "0181551570af73bba5977263d33e981a", 144 | "01815515_70af-73bb-a597-726461ccf7f4", 145 | "01815515-70af 73bb-a597-726523b5414d", 146 | "01815515-70af-73bb-a597-7266_8e4c2c3", 147 | "0181-515-70af-73bb-a597-726778070225", 148 | "01815515-70af-7 bb-a597-726849072769", 149 | "0181g515-70af-73bb-a597-72699ab58e63", 150 | "01815515-70af-73bb-a597-726ladf25973", 151 | "01815515-70af-73zb-a597-726b873b50aa", 152 | }; 153 | const int n_cases = sizeof(cases) / sizeof(cases[0]); 154 | 155 | for (int i = 0; i < n_cases; i++) { 156 | uint8_t uuid[16]; 157 | int err = uuidv7_from_string(cases[i], uuid); 158 | assert(err != 0); 159 | } 160 | } 161 | 162 | #ifndef NDEBUG 163 | int main(void) { 164 | test_unprecedented(); 165 | fprintf(stderr, " %s: ok\n", "test_unprecedented"); 166 | test_with_prev(); 167 | fprintf(stderr, " %s: ok\n", "test_with_prev"); 168 | test_from_to_string(); 169 | fprintf(stderr, " %s: ok\n", "test_from_to_string"); 170 | test_from_string_error(); 171 | fprintf(stderr, " %s: ok\n", "test_from_string_error"); 172 | 173 | return 0; 174 | } 175 | #endif 176 | -------------------------------------------------------------------------------- /test/test_new.c: -------------------------------------------------------------------------------- 1 | #include "uuidv7.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #define N_SAMPLES 100000 9 | static char samples[N_SAMPLES][40]; 10 | 11 | void setup(void) { 12 | for (int i = 0; i < N_SAMPLES; i++) { 13 | int status = uuidv7_new_string(samples[i]); 14 | assert(status >= 0); 15 | } 16 | } 17 | 18 | void test_format(void) { 19 | for (int i = 0; i < N_SAMPLES; i++) { 20 | assert(strlen(samples[i]) == 36); 21 | for (int j = 0; j < 36; j++) { 22 | char c = samples[i][j]; 23 | if (j == 8 || j == 13 || j == 18 || j == 23) 24 | assert(c == '-'); 25 | else if (j == 14) 26 | assert(c == '7'); 27 | else if (j == 19) 28 | assert(c == '8' || c == '9' || c == 'a' || c == 'b'); 29 | else 30 | assert(c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || 31 | c == '5' || c == '6' || c == '7' || c == '8' || c == '9' || 32 | c == 'a' || c == 'b' || c == 'c' || c == 'd' || c == 'e' || 33 | c == 'f'); 34 | } 35 | } 36 | } 37 | 38 | void test_order(void) { 39 | for (int i = 1; i < N_SAMPLES; i++) 40 | assert(strcmp(samples[i - 1], samples[i]) < 0); 41 | } 42 | 43 | void test_timestamp_and_counter(void) { 44 | uint8_t prev[16], curr[16]; 45 | uuidv7_from_string(samples[0], prev); 46 | for (int i = 1; i < N_SAMPLES; i++) { 47 | uuidv7_from_string(samples[i], curr); 48 | assert(memcmp(prev, curr, 6) < 0 || 49 | (memcmp(prev, curr, 6) == 0 && memcmp(&prev[6], &curr[6], 6) < 0)); 50 | memcpy(curr, prev, 16); 51 | } 52 | } 53 | 54 | void test_constant_and_random_bits(void) { 55 | // count '1' of each bit 56 | uint32_t bins[128] = {0}; 57 | for (int i = 0; i < N_SAMPLES; i++) { 58 | uint8_t uuid[16]; 59 | uuidv7_from_string(samples[i], uuid); 60 | for (int j = 0; j < 16; j++) { 61 | for (int k = 7; k >= 0; k--) { 62 | bins[j * 8 + k] += uuid[j] & 1; 63 | uuid[j] >>= 1; 64 | } 65 | } 66 | } 67 | 68 | // test if constant bits are all set to 1 or 0 69 | assert(bins[48] == 0); // version bit 48 70 | assert(bins[49] == N_SAMPLES); // version bit 49 71 | assert(bins[50] == N_SAMPLES); // version bit 50 72 | assert(bins[51] == N_SAMPLES); // version bit 51 73 | assert(bins[64] == N_SAMPLES); // variant bit 64 74 | assert(bins[65] == 0); // variant bit 65 75 | 76 | // test if random bits are set to 1 at ~50% probability 77 | // set margin based on binom dist 99.999% confidence interval 78 | double margin = 4.417173 * sqrt(0.5 * 0.5 / (double)N_SAMPLES); 79 | for (int i = 96; i < 128; i++) 80 | assert(fabs((double)bins[i] / (double)N_SAMPLES - 0.5) < margin); 81 | } 82 | 83 | #ifndef NDEBUG 84 | int main(void) { 85 | setup(); 86 | 87 | test_format(); 88 | fprintf(stderr, " %s: ok\n", "test_format"); 89 | test_order(); 90 | fprintf(stderr, " %s: ok\n", "test_order"); 91 | test_timestamp_and_counter(); 92 | fprintf(stderr, " %s: ok\n", "test_timestamp_and_counter"); 93 | test_constant_and_random_bits(); 94 | fprintf(stderr, " %s: ok\n", "test_constant_and_random_bits"); 95 | 96 | return 0; 97 | } 98 | #endif 99 | -------------------------------------------------------------------------------- /uuidv7.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file 3 | * 4 | * uuidv7.h - Single-file C/C++ UUIDv7 Library 5 | * 6 | * @version v0.1.6 7 | * @author LiosK 8 | * @copyright Licensed under the Apache License, Version 2.0 9 | * @see https://github.com/LiosK/uuidv7-h 10 | */ 11 | /* 12 | * Copyright 2022 LiosK 13 | * 14 | * Licensed under the Apache License, Version 2.0 (the "License"); 15 | * you may not use this file except in compliance with the License. 16 | * You may obtain a copy of the License at 17 | * 18 | * http://www.apache.org/licenses/LICENSE-2.0 19 | * 20 | * Unless required by applicable law or agreed to in writing, software 21 | * distributed under the License is distributed on an "AS IS" BASIS, 22 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 23 | * See the License for the specific language governing permissions and 24 | * limitations under the License. 25 | */ 26 | #ifndef UUIDV7_H_BAEDKYFQ 27 | #define UUIDV7_H_BAEDKYFQ 28 | 29 | #include 30 | #include 31 | 32 | /** 33 | * @name Status codes returned by uuidv7_generate() 34 | * 35 | * @{ 36 | */ 37 | 38 | /** 39 | * Indicates that the `unix_ts_ms` passed was used because no preceding UUID was 40 | * specified. 41 | */ 42 | #define UUIDV7_STATUS_UNPRECEDENTED (0) 43 | 44 | /** 45 | * Indicates that the `unix_ts_ms` passed was used because it was greater than 46 | * the previous one. 47 | */ 48 | #define UUIDV7_STATUS_NEW_TIMESTAMP (1) 49 | 50 | /** 51 | * Indicates that the counter was incremented because the `unix_ts_ms` passed 52 | * was no greater than the previous one. 53 | */ 54 | #define UUIDV7_STATUS_COUNTER_INC (2) 55 | 56 | /** 57 | * Indicates that the previous `unix_ts_ms` was incremented because the counter 58 | * reached its maximum value. 59 | */ 60 | #define UUIDV7_STATUS_TIMESTAMP_INC (3) 61 | 62 | /** 63 | * Indicates that the monotonic order of generated UUIDs was broken because the 64 | * `unix_ts_ms` passed was less than the previous one by more than ten seconds. 65 | */ 66 | #define UUIDV7_STATUS_CLOCK_ROLLBACK (4) 67 | 68 | /** Indicates that an invalid `unix_ts_ms` is passed. */ 69 | #define UUIDV7_STATUS_ERR_TIMESTAMP (-1) 70 | 71 | /** 72 | * Indicates that the attempt to increment the previous `unix_ts_ms` failed 73 | * because it had reached its maximum value. 74 | */ 75 | #define UUIDV7_STATUS_ERR_TIMESTAMP_OVERFLOW (-2) 76 | 77 | /** @} */ 78 | 79 | #ifdef __cplusplus 80 | extern "C" { 81 | #endif 82 | 83 | /** 84 | * @name Low-level primitives 85 | * 86 | * @{ 87 | */ 88 | 89 | /** 90 | * Generates a new UUIDv7 from the given Unix time, random bytes, and previous 91 | * UUID. 92 | * 93 | * @param uuid_out 16-byte byte array where the generated UUID is stored. 94 | * @param unix_ts_ms Current Unix time in milliseconds. 95 | * @param rand_bytes At least 10-byte byte array filled with random bytes. This 96 | * function consumes the leading 4 bytes or the whole 10 97 | * bytes per call depending on the conditions. 98 | * `uuidv7_status_n_rand_consumed()` maps the return value of 99 | * this function to the number of random bytes consumed. 100 | * @param uuid_prev 16-byte byte array representing the immediately preceding 101 | * UUID, from which the previous timestamp and counter are 102 | * extracted. This may be NULL if the caller does not care 103 | * the ascending order of UUIDs within the same timestamp. 104 | * This may point to the same location as `uuid_out`; this 105 | * function reads the value before writing. 106 | * @return One of the `UUIDV7_STATUS_*` codes that describe the 107 | * characteristics of generated UUIDs. Callers can usually 108 | * ignore the status unless they need to guarantee the 109 | * monotonic order of UUIDs or fine-tune the generation 110 | * process. 111 | */ 112 | static inline int8_t uuidv7_generate(uint8_t *uuid_out, uint64_t unix_ts_ms, 113 | const uint8_t *rand_bytes, 114 | const uint8_t *uuid_prev) { 115 | static const uint64_t MAX_TIMESTAMP = ((uint64_t)1 << 48) - 1; 116 | static const uint64_t MAX_COUNTER = ((uint64_t)1 << 42) - 1; 117 | 118 | if (unix_ts_ms > MAX_TIMESTAMP) { 119 | return UUIDV7_STATUS_ERR_TIMESTAMP; 120 | } 121 | 122 | int8_t status; 123 | uint64_t timestamp = 0; 124 | if (uuid_prev == NULL) { 125 | status = UUIDV7_STATUS_UNPRECEDENTED; 126 | timestamp = unix_ts_ms; 127 | } else { 128 | for (int i = 0; i < 6; i++) { 129 | timestamp = (timestamp << 8) | uuid_prev[i]; 130 | } 131 | 132 | if (unix_ts_ms > timestamp) { 133 | status = UUIDV7_STATUS_NEW_TIMESTAMP; 134 | timestamp = unix_ts_ms; 135 | } else if (unix_ts_ms + 10000 < timestamp) { 136 | // ignore prev if clock moves back by more than ten seconds 137 | status = UUIDV7_STATUS_CLOCK_ROLLBACK; 138 | timestamp = unix_ts_ms; 139 | } else { 140 | // increment prev counter 141 | uint64_t counter = uuid_prev[6] & 0x0f; // skip ver 142 | counter = (counter << 8) | uuid_prev[7]; 143 | counter = (counter << 6) | (uuid_prev[8] & 0x3f); // skip var 144 | counter = (counter << 8) | uuid_prev[9]; 145 | counter = (counter << 8) | uuid_prev[10]; 146 | counter = (counter << 8) | uuid_prev[11]; 147 | 148 | if (counter++ < MAX_COUNTER) { 149 | status = UUIDV7_STATUS_COUNTER_INC; 150 | uuid_out[6] = counter >> 38; // ver + bits 0-3 151 | uuid_out[7] = counter >> 30; // bits 4-11 152 | uuid_out[8] = counter >> 24; // var + bits 12-17 153 | uuid_out[9] = counter >> 16; // bits 18-25 154 | uuid_out[10] = counter >> 8; // bits 26-33 155 | uuid_out[11] = counter; // bits 34-41 156 | } else { 157 | // increment prev timestamp at counter overflow 158 | status = UUIDV7_STATUS_TIMESTAMP_INC; 159 | timestamp++; 160 | if (timestamp > MAX_TIMESTAMP) { 161 | return UUIDV7_STATUS_ERR_TIMESTAMP_OVERFLOW; 162 | } 163 | } 164 | } 165 | } 166 | 167 | uuid_out[0] = timestamp >> 40; 168 | uuid_out[1] = timestamp >> 32; 169 | uuid_out[2] = timestamp >> 24; 170 | uuid_out[3] = timestamp >> 16; 171 | uuid_out[4] = timestamp >> 8; 172 | uuid_out[5] = timestamp; 173 | 174 | for (int i = (status == UUIDV7_STATUS_COUNTER_INC) ? 12 : 6; i < 16; i++) { 175 | uuid_out[i] = *rand_bytes++; 176 | } 177 | 178 | uuid_out[6] = 0x70 | (uuid_out[6] & 0x0f); // set ver 179 | uuid_out[8] = 0x80 | (uuid_out[8] & 0x3f); // set var 180 | 181 | return status; 182 | } 183 | 184 | /** 185 | * Determines the number of random bytes consumsed by `uuidv7_generate()` from 186 | * the `UUIDV7_STATUS_*` code returned. 187 | * 188 | * @param status `UUIDV7_STATUS_*` code returned by `uuidv7_generate()`. 189 | * @return `4` if `status` is `UUIDV7_STATUS_COUNTER_INC` or `10` 190 | * otherwise. 191 | */ 192 | static inline int uuidv7_status_n_rand_consumed(int8_t status) { 193 | return status == UUIDV7_STATUS_COUNTER_INC ? 4 : 10; 194 | } 195 | 196 | /** 197 | * Encodes a UUID in the 8-4-4-4-12 hexadecimal string representation. 198 | * 199 | * @param uuid 16-byte byte array representing the UUID to encode. 200 | * @param string_out Character array where the encoded string is stored. Its 201 | * length must be 37 (36 digits + NUL) or longer. 202 | */ 203 | static inline void uuidv7_to_string(const uint8_t *uuid, char *string_out) { 204 | static const char DIGITS[] = "0123456789abcdef"; 205 | for (int i = 0; i < 16; i++) { 206 | uint_fast8_t e = uuid[i]; 207 | *string_out++ = DIGITS[e >> 4]; 208 | *string_out++ = DIGITS[e & 15]; 209 | if (i == 3 || i == 5 || i == 7 || i == 9) { 210 | *string_out++ = '-'; 211 | } 212 | } 213 | *string_out = '\0'; 214 | } 215 | 216 | /** 217 | * Decodes the 8-4-4-4-12 hexadecimal string representation of a UUID. 218 | * 219 | * @param string 37-byte (36 digits + NUL) character array representing the 220 | * 8-4-4-4-12 hexadecimal string representation. 221 | * @param uuid_out 16-byte byte array where the decoded UUID is stored. 222 | * @return Zero on success or non-zero integer on failure. 223 | */ 224 | static inline int uuidv7_from_string(const char *string, uint8_t *uuid_out) { 225 | for (int i = 0; i < 32; i++) { 226 | char c = *string++; 227 | // clang-format off 228 | uint8_t x = c == '0' ? 0 : c == '1' ? 1 : c == '2' ? 2 : c == '3' ? 3 229 | : c == '4' ? 4 : c == '5' ? 5 : c == '6' ? 6 : c == '7' ? 7 230 | : c == '8' ? 8 : c == '9' ? 9 : c == 'a' ? 10 : c == 'b' ? 11 231 | : c == 'c' ? 12 : c == 'd' ? 13 : c == 'e' ? 14 : c == 'f' ? 15 232 | : c == 'A' ? 10 : c == 'B' ? 11 : c == 'C' ? 12 : c == 'D' ? 13 233 | : c == 'E' ? 14 : c == 'F' ? 15 : 0xff; 234 | // clang-format on 235 | if (x == 0xff) { 236 | return -1; // invalid digit 237 | } 238 | 239 | if ((i & 1) == 0) { 240 | uuid_out[i >> 1] = x << 4; // even i => hi 4 bits 241 | } else { 242 | uuid_out[i >> 1] |= x; // odd i => lo 4 bits 243 | } 244 | 245 | if ((i == 7 || i == 11 || i == 15 || i == 19) && (*string++ != '-')) { 246 | return -1; // invalid format 247 | } 248 | } 249 | if (*string != '\0') { 250 | return -1; // invalid length 251 | } 252 | return 0; // success 253 | } 254 | 255 | /** @} */ 256 | 257 | /** 258 | * @name High-level APIs that require platform integration 259 | * 260 | * @{ 261 | */ 262 | 263 | /** 264 | * Generates a new UUIDv7 with the current Unix time. 265 | * 266 | * This declaration defines the interface to generate a new UUIDv7 with the 267 | * current time, default random number generator, and global shared state 268 | * holding the previously generated UUID. Since this single-file library does 269 | * not provide platform-specific implementations, users need to prepare a 270 | * concrete implementation (if necessary) by integrating a real-time clock, 271 | * cryptographically strong random number generator, and shared state storage 272 | * available in the target platform. 273 | * 274 | * @param uuid_out 16-byte byte array where the generated UUID is stored. 275 | * @return One of the `UUIDV7_STATUS_*` codes that describe the 276 | * characteristics of generated UUIDs or an 277 | * implementation-dependent code. Callers can usually ignore 278 | * the `UUIDV7_STATUS_*` code unless they need to guarantee the 279 | * monotonic order of UUIDs or fine-tune the generation 280 | * process. The implementation-dependent code must be out of 281 | * the range of `int8_t` and negative if it reports an error. 282 | */ 283 | int uuidv7_new(uint8_t *uuid_out); 284 | 285 | /** 286 | * Generates an 8-4-4-4-12 hexadecimal string representation of new UUIDv7. 287 | * 288 | * @param string_out Character array where the encoded string is stored. Its 289 | * length must be 37 (36 digits + NUL) or longer. 290 | * @return Return value of `uuidv7_new()`. 291 | * @note Provide a concrete `uuidv7_new()` implementation to enable 292 | * this function. 293 | */ 294 | static inline int uuidv7_new_string(char *string_out) { 295 | uint8_t uuid[16]; 296 | int result = uuidv7_new(uuid); 297 | uuidv7_to_string(uuid, string_out); 298 | return result; 299 | } 300 | 301 | /** @} */ 302 | 303 | #ifdef __cplusplus 304 | } /* extern "C" { */ 305 | #endif 306 | 307 | #endif /* #ifndef UUIDV7_H_BAEDKYFQ */ 308 | --------------------------------------------------------------------------------