mirror of
https://github.com/shaka-project/shaka-player.git
synced 2026-06-20 16:57:25 +03:00
fff5dd7ee7
This changes namespace exports to the more targetted exportDoc, adds exports where they are needed and removes them where they are not. Exporting of Uint8Array and String utils should be re-evaluated later. Change-Id: I9298e73a0a5ef026b6f2b1854488d2c359be10c1
70 lines
1.9 KiB
JavaScript
70 lines
1.9 KiB
JavaScript
/**
|
|
* @license
|
|
* Copyright 2015 Google Inc.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
goog.provide('shaka.util.StringUtils');
|
|
|
|
|
|
// TODO: re-evaluate exporting these once Player 2.0 is complete
|
|
/**
|
|
* @namespace shaka.util.StringUtils
|
|
* @exportDoc
|
|
* @summary A set of string utility functions.
|
|
*/
|
|
|
|
|
|
/**
|
|
* Convert a raw string to a base64 string. The output will always use the
|
|
* alternate encoding/alphabet also known as "base64url".
|
|
* @param {string} str
|
|
* @param {boolean=} opt_padding If true, pad the output with equals signs.
|
|
* Defaults to true.
|
|
* @return {string}
|
|
* @export
|
|
*/
|
|
shaka.util.StringUtils.toBase64 = function(str, opt_padding) {
|
|
var padding = (opt_padding == undefined) ? true : opt_padding;
|
|
var base64 = window.btoa(str).replace(/\+/g, '-').replace(/\//g, '_');
|
|
return padding ? base64 : base64.replace(/=*$/, '');
|
|
};
|
|
|
|
|
|
/**
|
|
* Convert a base64 string to a raw string. Accepts either the standard
|
|
* alphabet or the alternate "base64url" alphabet.
|
|
* @param {string} str
|
|
* @return {string}
|
|
* @export
|
|
*/
|
|
shaka.util.StringUtils.fromBase64 = function(str) {
|
|
return window.atob(str.replace(/-/g, '+').replace(/_/g, '/'));
|
|
};
|
|
|
|
|
|
/**
|
|
* Separates every 4 characters by a space.
|
|
* @param {string} str
|
|
* @return {string}
|
|
* @export
|
|
*/
|
|
shaka.util.StringUtils.formatHexString = function(str) {
|
|
return str.split('').reduce(
|
|
function(acc, ch, i) {
|
|
return acc + (i && (i % 4 == 0) ? ' ' + ch : ch);
|
|
});
|
|
};
|
|
|