Giter Site home page Giter Site logo

mezoni / string_reader Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 6 KB

Implementation of random access string readers for reading data from various data sources in various encodings through a single `StringReader` interface.

License: BSD 3-Clause "New" or "Revised" License

Dart 100.00%
dart dart3 dartlang file-reader string-reader

string_reader's Introduction

string_readers

Implementation of random access string readers for reading data from various data sources in various encodings through a single StringReader interface.

Version: 0.1.0

About this software

Implementation of random access string readers for reading data from various data sources in various encodings through a single StringReader interface.

Possible application of this is the organization of access operations to data located in large files, as well as the organization of access to data from various sources through a single StringReader interface.

The current version includes the following implementations:

  • ByteReader
  • FileReader
  • StringReader
  • Utf8Reader

These implementations allow random read access to string input data from the following sources:

  • String data encoded in Utf-16 encoding
  • Byte data encoded in Utf-8 encoding
  • String data located in files

Example

An example of accessing data from various sources through a single StringReader interface.

import 'dart:convert';
import 'dart:io';

import 'package:string_reader/file_reader.dart';
import 'package:string_reader/string_reader.dart';
import 'package:string_reader/utf8_reader.dart';

void main(List<String> args) {
  const source = 'Hello';
  File? file;
  RandomAccessFile? fp;
  try {
    const filename = 'example/temp.txt';
    file = File(filename);
    _writeToFile(file, source);
    fp = file.openSync();
    final result = _readUsingFileReader(fp);
    print(result);
  } catch (e) {
    rethrow;
  } finally {
    if (fp != null) {
      fp.closeSync();
    }

    if (file != null) {
      if (file.existsSync()) {
        file.deleteSync();
      }
    }
  }

  final stringReader = StringReader(source);
  final result = _readUsingStringReader(stringReader);
  print(result);
}

String _readUsingFileReader(RandomAccessFile fp) {
  const bufferSize = 8;
  final fileReader = FileReader(fp, bufferSize: bufferSize);
  final utf8Reader = Utf8Reader(fileReader);
  return _readUsingStringReader(utf8Reader);
}

String _readUsingStringReader(StringReader reader) {
  final charCodes = <int>[];
  var position = 0;
  while (position < reader.length) {
    final char = reader.readChar(position);
    position += reader.count;
    charCodes.add(char);
  }

  return String.fromCharCodes(charCodes);
}

void _writeToFile(File file, String string) {
  if (file.existsSync()) {
    file.deleteSync();
  }

  final bytes = Utf8Encoder().convert('Hello');
  file.writeAsBytesSync(bytes);
}

string_reader's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.