Home > Software design >  How to see dart core library code (StringBuffer)?
How to see dart core library code (StringBuffer)?

Time:09-16

I just wondered how the StringBuffer's concrete code is look like.

But I can only find abstract method like this. Well...

Could you please let me know how to get to the concrete code ? Thanks!

// Copyright (c) 2011, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

part of dart.core;

class StringBuffer implements StringSink {
  /// Creates a string buffer containing the provided [content].
  external StringBuffer([Object content = ""]);

  /// Returns the length of the content that has been accumulated so far.
  /// This is a constant-time operation.
  external int get length;

  /// Returns whether the buffer is empty. This is a constant-time operation.
  bool get isEmpty => length == 0;

  /// Returns whether the buffer is not empty. This is a constant-time
  /// operation.
  bool get isNotEmpty => !isEmpty;

  /// Adds the string representation of [object] to the buffer.
  external void write(Object? object);

  /// Adds the string representation of [charCode] to the buffer.
  ///
  /// Equivalent to `write(String.fromCharCode(charCode))`.
  external void writeCharCode(int charCode);

  /// Writes all [objects] separated by [separator].
  ///
  /// Writes each individual object in [objects] in iteration order,
  /// and writes [separator] between any two objects.
  external void writeAll(Iterable<dynamic> objects, [String separator = ""]);

  external void writeln([Object? obj = ""]);

  /// Clears the string buffer.
  external void clear();

  /// Returns the contents of buffer as a single string.
  external String toString();
}

CodePudding user response:

It depends on what target platform you are interested in which is also the reason why the do this since the external function can point to different implementation depending on target.

The implementation for the different platforms can be found here:
https://github.com/dart-lang/sdk/tree/2.18.1/sdk/lib/_internal

So if you want to see the implementation used when running the Dart VM or compiled to AOT binary, the implementation of StringBuffer can be found here:
https://github.com/dart-lang/sdk/blob/2.18.1/sdk/lib/_internal/vm/lib/string_buffer_patch.dart

Another point here is that Dart VM code often have stuff like the following which you can see in the bottom of the StringBuffer implementation:

  @pragma("vm:external-name", "StringBuffer_createStringFromUint16Array")
  external static String _create(Uint16List buffer, int length, bool isLatin1);

This means that we do a call to the C code in the Dart runtime (which is bundled together with your compiled app). So the _create call will end up calling the following method:
https://github.com/dart-lang/sdk/blob/2.18.1/runtime/lib/string.cc#L516-L534

If your target are instead JavaScript, the code used for that implementation (when compiled to a release ready JS bundle) can be found here:
https://github.com/dart-lang/sdk/blob/2.18.1/sdk/lib/_internal/js_runtime/lib/core_patch.dart#L632

  •  Tags:  
  • dart
  • Related