An open API service indexing awesome lists of open source software.

https://github.com/4nkitd/cpoch

numeric string compression
https://github.com/4nkitd/cpoch

c compression golang numeric php string

Last synced: 7 months ago
JSON representation

numeric string compression

Awesome Lists containing this project

README

          

# Cpoch Library

Cpoch is a numeric string compression algorithm that shortens numeric strings by substituting pairs of digits with single ASCII characters. This repository provides implementations of the Cpoch algorithm in Go, C, and PHP.

The core idea is to use a predefined map to convert two-digit strings (e.g., "78") into a corresponding character (e.g., 'N'). This can reduce the length of long numeric IDs, timestamps, or other numeric data, making them more manageable for display or transmission.

## Implementations

This project includes the following implementations:

- **Go**: The core logic is in `cpoch.go`, with tests in `cpoch_test.go`.
- **C**: A modular C library located in the `c/` directory, with a `Makefile` for easy compilation and testing.
- **PHP**: The core logic is in `cpoch.php`, with a separate test runner in `cpoch_test.php`.

---

## Go Implementation

The Go implementation is contained within `cpoch.go`. The testing logic has been separated into `cpoch_test.go` according to standard Go practices.

### Usage

To run the tests, execute the following command in the `cpoch` directory:

```sh
go test -v
```

### API

- `func EncodeCpoch(dataStr string) string`
Encodes a numeric string into its Cpoch representation. If a pair of digits is not found in the encoding map, or for a trailing single digit, the digit is passed through without change.

- `func DecodeCpoch(data string) string`
Decodes a Cpoch string back into its original numeric form. Characters not found in the decoding map are passed through as-is.

---

## C Implementation

The C implementation has been organized into a library in the `c/` directory. The core logic is in `cpoch.c`, and tests are in the `c/tests/` subdirectory.

### Building and Running Tests

A `Makefile` is provided for convenience. Navigate to the `c/` directory and run the following command:

```sh
cd c/
make test
```
This command will compile the library and the test runner, then execute the test suite.

### API

The C functions are declared in `cpoch.h`:

- `char* encode_cpoch(const char* data_str)`
Encodes a null-terminated numeric string.
**Note**: This function returns a dynamically allocated string that the caller must free using `free()`.

- `char* decode_cpoch(const char* data)`
Decodes a null-terminated Cpoch string.
**Note**: This function returns a dynamically allocated string that the caller must free using `free()`.

---

## PHP Implementation

The PHP implementation is available in `cpoch.php`. The testing logic has been separated into `cpoch_test.php`.

### Testing

To run the PHP test suite, execute the test script from the command line:

```sh
php cpoch_test.php
```

### Usage

To use the functions in your own project, include the `cpoch.php` file:

```php
"Ncye586S98N7"

$decoded = decodeCpoch("Ncye586S98N7");
// => "783177515862598787"
```

### API

- `function encodeCpoch(string $dataStr): string`
Encodes a numeric string into its Cpoch representation.

- `function decodeCpoch(string $data): string`
Decodes a Cpoch string back into its original numeric form.